2015-04-07 54 views
0

我实现的一个小游戏(基本游戏状态),我也很难用按钮导航。 我有一个类包含3个按钮的菜单:播放,如何播放和退出。 例如,当我点击How To Play按钮时,它会将我重定向到How To Play类。 在如何玩类我只有一个按钮:返回这是假设重定向我回到Menu类。 一切正常,但当我点击后退按钮它退出游戏,这是因为它具有与菜单中的退出按钮相同的坐标。 如何阻止传播? 我的代码是这样的:Slick2D鼠标事件传播

Main Class: 

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{ // update the images on screen to create image movement illusion 
    Input input = gc.getInput(); 
    mouseX = Mouse.getX(); 
    mouseY = gc.getHeight()-Mouse.getY(); 
    position = "*xpos: "+mouseX+" ypos:"+mouseY; 
    buttonsListener(input, sbg); 
} 
public void buttonsListener(Input input, StateBasedGame sbg) throws SlickException 
{ 

    if((mouseX>130&&mouseX<477)&&(mouseY>308&&mouseY<377))//play button 
     if(input.isMouseButtonDown(0)) 
      sbg.enterState(1); 

    if((mouseX>130&&mouseX<477)&&(mouseY>408&&mouseY<477))//how to play button 
     if(input.isMouseButtonDown(0)) 
      sbg.enterState(2); 

    if((mouseX>130&&mouseX<477)&&(mouseY>508&&mouseY<577))//exit button 
     if(input.isMouseButtonDown(0)) 
      System.exit(0); 
} 

和:

HowToPlay Class: 

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{ 
    Input input = gc.getInput(); 
    mouseX = Mouse.getX(); 
    mouseY = gc.getHeight()-Mouse.getY(); 

    if((mouseX>130&&mouseX<477)&&(mouseY>508&&mouseY<577))//back button 
     if(input.isMouseButtonDown(0)) 
      sbg.enterState(0); 
} 

请帮忙,谢谢。

回答

0

我想通了,所以对于任何你有同样的问题,你可以简单地覆盖的mousePressed方法一样(在我的情况):

Main class: 
public void mousePressed(int button, int x, int y) { // check if the buttons are pressed 
     if((x>130&&x<477)&&(y>308&&y<377)) 
      if(button==0) 
       sbg.enterState(1); 
     if((x>130&&x<477)&&(y>408&&y<477)) 
      if(button==0) 
       sbg.enterState(2); 
     if((x>130&&x<477)&&(y>508&&y<577)) 
      if(button==0) 
       System.exit(0); 
    } 

而且

Howtoplay class: 
public void mousePressed(int button, int x, int y) { // check if the buttons are pressed 
    if((x > 130 && x < 477) && (y > 508 && y < 577)) 
     if(button==0) 
      sbg.enterState(0); 
} 

它将不再传播。