YungCheng Su
YungCheng Su

Reputation: 193

How can I stop the Processing Draw loop by click mouse right button?

I want to set a game.

When I press "s" key, then start the game. After I pressed the mouse right button, It will reset and stop the game. Now the problem is when I click right mouse button, the variable "game" should be set to false, but it still to be true when I release mouse button.

Anyone can help me? Thanks.

boolean game = false;

void setup() {
  size(640, 480);
}

void draw() {
  background(200, 200, 200);

  if (key=='s') game=true;
  println("1::" + game);

  if (game==true) {
    // gaming...

    if (mousePressed && mouseButton == RIGHT) {
      // I want to reset the game...
      game=false;

      println("2 after pressed right mouse button::" + game);
    }

    println("3 why still run the code here after I press mouse right button? ");
  }
}
void mouseDragged()
{
  if (game==true) {
    // the dragging game...
    print("3:: dragging" + game);
  }
}

Upvotes: 2

Views: 507

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

Use the mousePressed() function instead of the mousePressed variable:

The mousePressed() function is called once after every time a mouse button is pressed

void draw() {
    background(200, 200, 200);

    // DELETE
    //if (key=='s') game=true;
    //println("1::" + game);

    if (game==true) {
        // gaming...

        // DELETE
        //if (mousePressed && mouseButton == RIGHT) {
        //    game=false;
        //    println("2 after pressed right mouse button::" + game);
        //}

        println("3 why still run the code here after I press mouse right button? ");
    }
}
void keyPressed() {
    if (!game and key == 's') {
        game = true;
        println("1::" + game);
    }
}
void mousePressed() {
    if (game && mouseButton == RIGHT) {
        game = false;
        println("2 after pressed right mouse button::" + game);
    }
}

Upvotes: 1

Related Questions