带有Switch语句的主菜单 [英] main menu with switch statement

查看:49
本文介绍了带有Switch语句的主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,对于一个学校项目,我必须用一个名为‘Processing’的程序创建一个游戏。 我正在使用Switch语句创建一个主菜单。为此,我想使用按钮‘Start’、‘Help’和‘Exit’。我想使用这些按钮来更改Switch语句的变量。因此,我使用"MICESEPRESSED"。问题是,我按的是哪个按钮,给我的结果和‘退出’按钮一样。有没有人能给我一些建议,教我如何更好地组织我的菜单,甚至让我的按钮工作起来?我正在使用一个名为‘ControlP5’的处理库来制作按钮。

以下是我到目前为止的代码:

int mode; // 1: intro screen, 2: game  , 3: game over 

final int INTRO    = 1;
final int PLAY     = 2;
final int GAMEOVER = 3;

//==============================================================
void setup(){
size(1920,1080);
mode = 1;
}
//========================================================

void draw(){

  if(mode == 1){
    introScreen();   
  }
  else if (mode == 2){
    gameItself();   
  } 
   else if (mode == 3){
     gameOver();   
   }
   else println("mode error");{
   } 
}

void introScreen(){
mode = 1;
static int Page = 0;
import controlP5.*;


ControlP5 cp5;


 cp5= new ControlP5(this);
 switch(Page){

     case 0: // main menu
     cp5.addButton("Start").setValue(0).setPosition(1420,250).setSize(400,100);

     cp5.addButton("Exit").setValue(0).setPosition(1420,650).setSize(400,100);

     cp5.addButton("Help").setValue(0).setPosition(1420,450).setSize(400,100);
       break;

     case 1: //help menu

      cp5.addButton("Back").setValue(0).setPosition(1420,450).setSize(400,100);
        break;
   }



public void Start(){
 if(mousePressed){
   mode = 2; // switching to the game itself 
 }
  println("Start");
  }

  public void Exit(){
   if(mousePressed){
  exit(); }
  println("Exit");
  }

 public void Help(){


     Page = 1;

  println("Help");
  }

  public void Back(){
  if(mousePressed){
    Page  = 0;
  }
  println("Back");
  }




  void gameItself(){
// game and stuff
}

void gameOver(){
//gameover 
}

推荐答案

查看mousePressed事件的工作原理。您可能会使用此信息。

要实现通过单击按钮更改Page变量的目标,有多个选项。首先,我将使用更简单的,不需要导入但只需检查坐标的那个。然后我将执行相同的操作,但使用的是控件P5。

1.只是检查点击的位置

我将使用最基本的方法:检测"点击"并检查它的坐标是否在按钮内。

首先,我们将添加mouseClicked()方法。每次按下鼠标按钮时都会调用此方法。

// I'm typing this out of IDE si there may be some quirks to fix in the code
void mouseClicked() {
  switch(Page) {
    case 0:
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 250 && mouseY < 250+100) {
        // You're in the main menu and Start was clicked
      }
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 650 && mouseY < 650+100) {
        // You're in the main menu and Exit was clicked
      }
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
        // You're in the main menu and Help was clicked
      }
      // You should use 'else if' instead of 3 different if, but I coded it like that so it would be easier to see the small differences between the coordinates
    case 1:
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
        // You're un the help menu and Back was clicked
      }
  }
}

如您所见,我只是使用您的按钮的坐标和大小来检查单击是否位于其中。这是一种忍者的方式让我摆脱这个问题。我不知道您对编程的了解有多深,否则我会建议您构建一个处理用户输入的类,但对于像家庭作业这样的小练习来说,这种方法很容易管理。

2.设计控件P5按钮

我不是ControlP5专家,因此我们将密切关注基本知识。

我会直截了当地说,您提供的代码充满了问题,而且没有太多行,所以我不会指出哪里出了问题,而是给您一些可以工作的框架代码,您可以在这些代码上建立一些理解。我会给你工具,然后你就可以让你的项目工作了。

当您设计按钮时,如果您在同一对象中设计它们,它们将共享一些属性。例如,您的所有按钮将同时可见或不可见。您不需要一直重新绘制它们,因为它们已经处理了这一点,所以您需要使用其他方法来管理它们。

您应该将按钮设计为全局对象(就像您所做的那样),并将它们添加到ControlP5对象中,这是最有意义的。如果你愿意,你可以让每个对象有一个按钮,或者如果它们链接在一起,你可以有多个按钮,例如,同时出现的所有"菜单"按钮可能属于同一个对象。如果您只为整个程序设计一次按钮,请使用setup()方法设计按钮。当然,如果这不仅仅是一个家庭作业,您可能希望避免按钮是全局的,但将它们保存在内存中以用于短项目会容易得多。

按钮的"名称"也是当您单击它时它将尝试调用的方法的名称。两个按钮不能共享相同的"名称"。按钮可以有一个设置值,该值将被发送到它们调用的方法。

您不需要使用Processing的鼠标事件来使按钮工作。它们是自给自足的:它们有自己的事件,比如被点击或检测鼠标何时经过它们。Here's the documentation for the full list of the methods included in the ControlP5 buttons

您不需要管理draw()循环中的按钮。他们自己管理。

下面是一些框架代码,用于演示我刚才所说的内容。您可以将其复制并粘贴到新的处理项目中,然后运行它以查看正在进行的操作。

ControlP5 cp5;
ControlP5 flipVisibilityButton;
int Page = 0;

void setup() {
  size(1920, 800);
  textAlign(CENTER, CENTER);
  textSize(60);
  fill(255);

  cp5 = new ControlP5(this);    // this is ONE object, which will own buttons.
  cp5.addButton("MenuButton0")  // this is the name of the button, but also the name of the method it will call
    .setValue(0)  // this value will be sent to the method it calls
    .setPosition(1420, 250)
    .setSize(400, 100);
  cp5.addButton("MenuButton1")
    .setValue(1)
    .setPosition(1420, 450)
    .setSize(400, 100);
  cp5.addButton("MenuButton2")
    .setValue(2)
    .setPosition(1420, 650)
    .setSize(400, 100);

  flipVisibilityButton = new ControlP5(this);  // this is a different object which own it's own controls (a button in this case)
  flipVisibilityButton.addButton("flipVisibility")
    .setValue(2)
    .setPosition(200, height/2)
    .setSize(200, 100);
}

void draw() {
  // No button management to see here
  background(0);
  // showing which button has been pressed while also keeping watch to see if the mouse is over one of the cp5 buttons
  text(Page + "
" + cp5.isMouseOver(), width/2, height/2);
}

void MenuButton0(int value) {
  ChangePage(value);
}

void MenuButton1(int value) {
  ChangePage(value);
}

void MenuButton2(int value) {
  ChangePage(value);
}

void ChangePage(int value) {
  Page = value;
}

void flipVisibility(int value) {
  // When the buttons are invisible, they are also unclickable
  cp5.setVisible(!cp5.isVisible());
}

您应该能够在此示例上展开您的项目,但如果您有困难,请毫不犹豫地在此处进行评论并提出进一步的问题。玩得开心!

这篇关于带有Switch语句的主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆