How to pause part of a draw loop

I’m trying to make my character be able to crouch so i have

void draw ()


void keyPressed()
if (key == ‘s’)
image(box.image3, box.position.x, box.position.y);

it all works fine but the draw loop instantly replaces image 3 with image 1
if i pause the whole of draw then it places it and it isnt immediately replaced but i can still see image1 in the background because its no longer doing the background every frame is ther ea way to pause part of draw?

Better way : in keyPressed set a boolean variable crouch to true

Define it as global (aka before setup())

in keyPressed say crouch = true;

Evaluate it in draw() and display one image or the other
Use if(crouch) image...; else image....;

(keyPressed() is only called once, so no use to draw anything there. draw() runs 60 times per second)

Use keyReleased() to say crouch = false; then he crouches as long as you hold the key down.

1 Like


When box is a class that you wrote it is better to write a function display() inside the function and just call the function. Then crouch variable wouldn’t be global but part of the class.

The if clause I described would be inside of display() function.

Much better readable.


instead of

image(box.image3, box.position.x, box.position.y);

you would have inside the class

image(image3, position.x, position.y);

Somehow behind this a } is missing…?

Please format your code