处理 - mousePress图像转到功能

时间:2017-04-19 00:47:19

标签: menu processing imagebutton loadimage mousepress

我已经用menuStart.png和menuExit.png加载并创建了两个图像按钮menuStart和menuExit:

image(menuStart,250,350,100,42);

image(menuExit,450,345,110,45);

我在做什么: 我把我的页面设置为阶段。阶段1是菜单,阶段2是开始屏幕阶段3是选择难度屏幕+游戏,阶段4是退出屏幕,阶段5退出游戏。我想使用mousePressed函数,这样如果用户选择阶段1中的menuStart按钮,stage = 2.同样,如果用户选择阶段1中的menuExit按钮,则阶段= 5.

我用代码做了什么: 我已经实现了mousePressed,但不知道如何在image参数中设置mousePressed的参数。我该如何设置呢?

代码:

void doMenu() {
  // Stage 1 Start -- MENU
  if (stage == 1) {
    textFont(title);
    text("Game", 150, 200);
    textFont(subtitle);
    image(menuStart, 250, 350, 100, 42);
    image(menuExit, 450, 345, 110, 45);
    mousePressed();

    if(mousePressed == true) {
      stage = 2;
    }
  }
  // Stage 2 START
  if (stage == 2) {
    background(255);
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500);

    if(mousePressed == true) { // true -->start
      stage = 3; // go-to stage 3
    }
    /* else if(mousePressed == exit && stage != 2 { // exit
      stage = 5; // go-to exit
    }
    */
  }
  if(stage == 3) {
    background(255);
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500);
    text("Press N for Normal or H for Hard", 200, 375);

    if(mousePressed == true) { // true --> hard
      hard = true;
      normal = false;
      startMenu = false;
    }
    /*
    else if(mousePressed == normal) { // normal
      hard = false;
      normal = true;
      startMenu = false;
    }
    */
    /*
    if(mousePressed == true) { // easy
      hard = false;
      normal = true;
      startMenu = false;
    }
    */
  }
  // Stage 4 EXIT
  if (stage == 4) { 
  background(0);
  exitScreen = loadImage("exit.jpg"); 
  image(exitScreen, 0, 0, 800, 400);
  textFont(subtitle);
  text("Press X to Exit", 300, 375);
  if(mousePressed == true) {
      stage = 5;
    }
  }
  if(stage == 5) {
    exit();
  }
}

1 个答案:

答案 0 :(得分:0)

mousePressed变量无法按您描述的方式工作。它只是一个boolean变量,在按下鼠标时保持true,而在不按下时保持false。它不包含有关被点击内容的任何信息。

要检查被点击的内容,您将不得不使用mouseXmouseY变量以及if语句来检查光标是否在按钮内。一个简单的例子如下:

if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){
   //mouse is inside button
}

如果按下鼠标并且鼠标在按钮内,则按下按钮。您可以使用嵌套的if语句来检查这一点,也可以使用mousePressed()函数:

void mousePressed(){
    if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){
       //mouse is clicking button
    }
}

无耻的自我推销:here是关于在Processing中获取使用输入的教程。