【问题标题】:How can I stop the Processing Draw loop by click mouse right button?如何通过单击鼠标右键停止处理绘制循环?
【发布时间】:2021-10-16 17:27:00
【问题描述】:

我想设置一个游戏。

当我按下“s”键,然后开始游戏。 在我按下鼠标右键后, 它将重置并停止游戏。 现在的问题是当我单击鼠标右键时, 变量“game”应该设置为false, 但是当我释放鼠标按钮时它仍然是正确的。

有人可以帮助我吗?谢谢。

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);
  }
}

【问题讨论】:

    标签: processing


    【解决方案1】:

    使用mousePressed() 函数代替mousePressed 变量:

    mousePressed() 函数在每次按下鼠标按钮后调用一次

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      相关资源
      最近更新 更多