A bui bug, i think

I'm using bui for my game menus, and i think i've found a bug: i have this code:



startButton.addListener(

new ActionListener(){

public void actionPerformed(ActionEvent arg0) {

if(startMenuInput.isEnabled() && arg0.getSource() == startButton)

GameManager.setGameState(GAMESTATE.LEVEL, levelInput, gameInput, startMenuInput, pauseMenuInput);

}

});



In a button. The code changes the state of the game to LEVEL (changes the updated node) and enables the levelInput and disables the other ones (set enabled = false).

The first problem is that this wasn't enough to disable the listener, so i put the startMenuInput.isEnabled() && arg0.getSource() == startButton in the condition above.

The next problem is that when this menu is later on activated the second time,

by a GameManager.setGameState(GAMESTATE.START_MENU,  startMenuInput, gameInput, levelInput, pauseMenuInput);

the actionlisterner above fires like mad, as if the button was permantly pressed.

Can you give some ideas for a solution?

i suggest you set an action command for your button.

i didn't understand the first problem, but i think i can help with the second. there are several things that trigger the listeners of a BComponent (such as mouse movement, state changes: enabled/disabled). so probably your listener is triggered again by the state change, which probably triggers another state change, which triggers the trigger again, … etc. (just speculating).



startButton = new BButton("Start", "start command");
startButton.addActionListener( new ActionListener() {
  public void actionPerformed(ActionEvent event) {
    // because this is an anonymous class, the component which triggers this method should be startButton
    // so you only have to check if the proper command is triggered and not some other even like state changes
    if("start command".equals(event.getAction() )
      GameManager.setGameState(GAMESTATE.LEVEL, levelInput, gameInput, startMenuInput, pauseMenuInput);
    }
  }
);



i don't know what startMenuInput is, so you might want to add it to the code above if it's important :P
let me know if this helped

Humm, no it didn't do anything more than what i was already doing. The the input variables are Input handlers, and the first is setEnable(true) and the others setEnabled(false). The function also changes the Node that the main loop updates.



I think the problem might originate in that i'm creating two Bwindows

here is the code:

As you can see they are almost equal, only the nodes in the GameManager change.

The code as its shown bellow with you suggested alteration invokes the unpause button when in the pause node, but it also invokes the start button (restarting the game)

I thought i might make the menus act on different inputHandlers and the buttons would not be invoked on top one of eachother and they don't, but now, when i return to the

start menu, from my game node, the game behaves as if i'd immediatly pressed the start game button. Maybe i'm not calling one of the updateXXX functions? Does BUI or JME needs those when changing the updated nodes?



public void createStartMenu(){
   
   GameManager.startMenu.setRenderQueueMode( Renderer.QUEUE_ORTHO );
   GameManager.startMenu.setLightCombineMode( LightState.OFF );
   
   PolledRootNode guiRoot = new PolledRootNode(this.timer, this.startMenuInput);
   GameManager.startMenu.setCullMode( SceneElement.CULL_NEVER );
   GameManager.startMenu.attachChild( guiRoot );
   
   BStyleSheet style = null;
   try {
      InputStream stin = getClass().getClassLoader()
      .getResourceAsStream("rsrc/style.bss");
      style = new BStyleSheet(new InputStreamReader(stin),
            new BStyleSheet.DefaultResourceProvider());
   }
   catch (Exception e) {
      e.printStackTrace(System.err);
      System.exit(-1);
   }
   
   BWindow window = new BDecoratedWindow(style, null);
   window.setLayoutManager(new AbsoluteLayout());
   window.setSize(width, height);
   window.center();
   
   final BButton startButton = new BButton("Start", "start command");
   
   startButton.setSize(50, 20);
   final BButton quitButton = new BButton("Quit", "quit command");
   quitButton.setSize(50, 20);
   
   window.add(startButton, new Point(width/2 - startButton.getWidth() - 10, height/2) );
   window.add(quitButton, new Point(width/2 + 10 + quitButton.getWidth(), height/2));
   
   guiRoot.addWindow(window);
   
   startButton.addListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
               //if(startMenuInput.isEnabled() && arg0.getSource() == startButton)
               if("start command".equals(arg0.getAction() )   )
               GameManager.setGameState(GAMESTATE.LEVEL, levelInput, gameInput, startMenuInput, pauseMenuInput);
            }               
         });
   
   quitButton.addListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
               //if(startMenuInput.isEnabled() && arg0.getSource() == quitButton)
               if("quit command".equals(arg0.getAction() ))
                  finish();
            }   
         });
   
   GameManager.runOnStartMenu = new Runnable(){
      public void run() {
         MouseInput.get().setCursorVisible( true );
      }
   };
   
   GameManager.runOnLeaveStartMenu = new Runnable(){
      public void run() {
         
      }
   };
   
   GameManager.startMenu.updateRenderState();
}

/**
 * Create the pause menu:
 */
public void createPauseMenu(){
   
   GameManager.pauseMenu.setRenderQueueMode( Renderer.QUEUE_ORTHO );
   GameManager.pauseMenu.setLightCombineMode( LightState.OFF );
   
   PolledRootNode guiRoot = new PolledRootNode(this.timer, this.pauseMenuInput);
   GameManager.pauseMenu.setCullMode( SceneElement.CULL_NEVER );
   GameManager.pauseMenu.attachChild( guiRoot );
   
   BStyleSheet style = null;
   try {
      InputStream stin = getClass().getClassLoader()
      .getResourceAsStream("rsrc/style.bss");
      style = new BStyleSheet(new InputStreamReader(stin),
            new BStyleSheet.DefaultResourceProvider());
   }
   catch (Exception e) {
      e.printStackTrace(System.err);
      System.exit(-1);
   }
   
   BWindow window = new BDecoratedWindow(style, null);
   window.setLayoutManager(new AbsoluteLayout());
   window.setSize(width, height);
   window.center();
   
   final BButton unpauseButton = new BButton("Unpause", "unpause command");
   unpauseButton.setSize(50, 20);
   final BButton quitButton = new BButton("Quit", "quit command2");
   quitButton.setSize(50, 20);
   
   window.add(unpauseButton, new Point(width/2 - unpauseButton.getWidth() - 10, height/2) );
   window.add(quitButton, new Point(width/2 + 10 + quitButton.getWidth(), height/2));
   
   guiRoot.addWindow(window);
   
   unpauseButton.addListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
               //if(pauseMenuInput.isEnabled() && arg0.getSource() == unpauseButton)
               if("unpause command".equals(arg0.getAction() ))
                  GameManager.setGameState(GAMESTATE.GAME, gameInput, pauseMenuInput, startMenuInput, levelInput);
               
               
               
            }
            
         });
   
   quitButton.addListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
               //if(pauseMenuInput.isEnabled() && arg0.getSource() == quitButton)
               if("quit command2".equals(arg0.getAction() ))
                  finish();
            }
            
         });
   
   
   GameManager.runOnPauseMenu = new Runnable(){
      public void run() {
         //save the game for tranparent goodness
         GameManager.game.lock();
         GameManager.pauseMenu.attachChild(GameManager.game);
         MouseInput.get().setCursorVisible( true );
      }
   };
   
   GameManager.runOnLeavePauseMenu = new Runnable(){
      public void run() {
         //remove from menu when restarting.
         GameManager.game.unlock();
         GameManager.game.removeFromParent();
         
      }
   };
   
   
   GameManager.pauseMenu.updateRenderState();
}


sorry for answering so late. i couldn't find anything wrong with the code.

did you try putting some breakpoints in the startButton listener to see where those events coem from? (take a look at the stack trace)

The error was mine, i guess. One of the runOnXXX runables invoked a state change (when there was no more levels an exception was caught on the runables of the level change, and the state changed to the start menu). I delegated that to the main game loop and the error dissapeared.

well, i'm glad that you could fix your problem  :wink: