The rectangle is moving down the screen! I want the rectangle to stop and start with the Space Bar

Please help me create this program I have done with the steps of moving rectangle up and down . Just confused with using spacebar for starting and stopping it.

1 Like

The Space Bar could switch a boolean variable named moving to true or false.

Set the variable

if(key==' ') 
     moving = ! moving;  // The "!" means "not", so we toggle moving here

Evaluate the variable

Then evaluate this variable moving to move the rectangle or not :

if(moving) 
    y = y + 3;

Declare the variable

before setup() say:

boolean moving=true;

2 Likes

thanks it really helped:))

1 Like