Java 状态管理-[切换状态,重放游戏]

Java 状态管理-[切换状态,重放游戏],java,user-input,slick2d,2d-games,state-management,Java,User Input,Slick2d,2d Games,State Management,如何有效地切换状态?每当我按下回放按钮时,两种状态交替显示(赢&玩)。我相信这是一个无限循环,但Eclipse不会打印任何错误 当我尝试时,结果为空。因此,结束游戏。 答案可能是什么?但是我不理解他的更新()。到底放什么?这会覆盖state类的更新吗 这是我的代码: stateID(2)是Wins.java 游乐场.java public static boolean bouncy = true; public void update(GameContainer gc, Sta

如何有效地切换状态?每当我按下回放按钮时,两种状态交替显示(赢&玩)。我相信这是一个无限循环,但Eclipse不会打印任何错误

当我尝试时,结果为空。因此,结束游戏。 答案可能是什么?但是我不理解他的更新()。到底放什么?这会覆盖state类的更新吗

这是我的代码: stateID(2)是Wins.java

游乐场.java

   public static boolean bouncy = true;   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws            SlickException{
             //if the user is successful, show winner state
            if(!bouncy){
                sbg.enterState(2);
            }
           //moves the image randomly in the screen
        new Timer(10,new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                  posX = (r.nextInt(TestProject4.appGC.getWidth()-75));
                  posY = (r.nextInt(TestProject4.appGC.getHeight()-75));
              }
         }).start();


    }          
    public void mousePressed(int button,int x,int y){
         //if the user pressed the mouse button And
         //if the user's coordinates is inside the hit area(rect)
        if((button == 0) && (rect.contains(x, y))){
            Toolkit.getDefaultToolkit().beep();
              bouncy = false;
        }
    }
    @Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
    //replay game
    if(backToGame == true){
        sbg.enterState(state);
    }
}

public void mousePressed(int button,int x,int y){
     //if the user clicks the replay button
    if(button == 0){        
      if((x>160 && x<260)&&(y>280 && y<320)){
             if(Mouse.isButtonDown(0)){
                    backToGame = true;                      
                    state = 1;
             }
          }

          //exit button
          if((x>=360 && x<460)&&(y>280 && y<320)){
             if(Mouse.isButtonDown(0)){
                System.exit(0);
          }
       }
    }
}

Wins.java

   public static boolean bouncy = true;   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws            SlickException{
             //if the user is successful, show winner state
            if(!bouncy){
                sbg.enterState(2);
            }
           //moves the image randomly in the screen
        new Timer(10,new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                  posX = (r.nextInt(TestProject4.appGC.getWidth()-75));
                  posY = (r.nextInt(TestProject4.appGC.getHeight()-75));
              }
         }).start();


    }          
    public void mousePressed(int button,int x,int y){
         //if the user pressed the mouse button And
         //if the user's coordinates is inside the hit area(rect)
        if((button == 0) && (rect.contains(x, y))){
            Toolkit.getDefaultToolkit().beep();
              bouncy = false;
        }
    }
    @Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
    //replay game
    if(backToGame == true){
        sbg.enterState(state);
    }
}

public void mousePressed(int button,int x,int y){
     //if the user clicks the replay button
    if(button == 0){        
      if((x>160 && x<260)&&(y>280 && y<320)){
             if(Mouse.isButtonDown(0)){
                    backToGame = true;                      
                    state = 1;
             }
          }

          //exit button
          if((x>=360 && x<460)&&(y>280 && y<320)){
             if(Mouse.isButtonDown(0)){
                System.exit(0);
          }
       }
    }
}
@覆盖
公共无效更新(GameContainer gc、StateBasedGame sbg、int delta)引发异常{
//重播游戏
if(backToGame==true){
sbg.企业(州);
}
}
按下公共无效鼠标(int按钮、int x、int y){
//如果用户单击replay按钮
如果(按钮==0){

如果((x>160&&x280&&y=360&&x280&&y我想你会一次又一次地在两种状态之间切换,因为当你切换回来时,两个游戏状态对象仍然处于相同的“状态”(变量值仍然相同)

尝试:


先前的答复:

请检查您的if声明:

  if((x>160 && x<180)||(y>280 && y<320)){

您的代码有两个方法mousePressed(),一个在playway.java中,另一个在Wins.java中。 应用程序显然在使用playway.java代码

将按钮检测代码添加到playway.java中可以解决这个问题

另一个明智的方法是在Wins from playway.java中显式调用mousePressed()方法 也许也行

添加注释帮助。 它可以展示你的项目思路,帮助他人尝试并了解你在做什么。 在充实一个复杂的方法时,我一开始评论过多,通过反复调试和代码审查来剔除明显的评论。 当其他人查看您的代码时,我也会提供帮助。
请记住,没有愚蠢的评论,只有愚蠢的人。

谢谢你的建议,但这并没有解决我的问题:(我删除了这些评论,因为其他人可能会感到厌烦或其他什么。我现在将编辑和添加评论。顺便说一句,谢谢!谢谢@user3931253的建议。我尝试过,但没有改变:(顺便说一句,我编辑了我的问题陈述。