Processing: mousePressed function square should move from x=0 to x=100

Viewed 33

how can I make it, that I hit the mouseButton once, and the square is moving till x=100? It's only working when I'm holing mouse1 pressed. I want it, that ur pressing mouse1 and the square is moving till x= 100.

int rectX = 0;
void setup () {
size (200, 200);
} 
void draw () {
background (100);
stroke (255);
fill (180);
rect (rectX, 100, 20, 20);


if (mousePressed && rectX < 100)
rectX += 1;

}

void mousePressed () {
}

thanks

1 Answers

You'd want to track whether the square should move in a separate boolean variable. Something like this:

boolean move = false;

void draw(){
  if(move){
    // move your square
  }
}

void mousePressed(){
  move = true;
}
Related