1
\$\begingroup\$

I have question about game UI. Im using only one Screen class in my game and for different "screens" Im using gamestates. My question is how to set stage with buttons (play button, pause button, restart button) with custom listeners for each state? for example my play button should have 2 different listeners. First of them start the game if gamestate = menu and second unpause game when gamestate = pause.

What should I do ? make one playButton with "if" operator for different game states? or make two different playButton? and what with table? add all the buttons to one table and clear table each time when game state is changing? or make a few tables?

\$\endgroup\$
1
\$\begingroup\$

Create two separate listeners and plug them when you switch your stage/gamemode. Like this:

ClickListener gameMode1Listener = new ClickListener(){
    @Override
    public void clicked(InputEvent event, int x, int y){
        super.clicked(x,y)

        // here goes your behaviour for 1st game mode
    }
}

ClickListener gameMode2Listener = new ClickListener(){
    @Override
    public void clicked(InputEvent event, int x, int y){
        super.clicked(x,y)

        // here goes your behaviour for 2nd game mode
    }
}

Now if you have your button you can set a listener you want by calling:

someButton.addListener(gameMode1Listener);

If your game changes and you need a second behaviour for a button, you can call:

someButton.removeListener(gameMode1Listener); // not sure what's the name of the 'remove' method
someButton.addListener(gameMode2Listener);
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$
  • If you don't have too many buttons/tables and the project is relatively small then using different "if"s is perfectly fine.

  • If you have several of them then it would be better to have a function that is called whenever the state is changed and have it switch the listeners and tables.

  • An even better approach would be to create different groups, each with their own buttons and tables, and switch the group whenever the state of the games has changed. This approach sacrifices some memory but should make for faster switches.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I have 3 small buttons (play, pause, restart) and 1 big button (fullscreen) in background for control (for example if i click on background my character jump). Can you show me example of using group with for example one own table and one button? \$\endgroup\$ – Land93 Oct 6 '16 at 14:44
  • \$\begingroup\$ I don't have any example atm but it should be pretty simple. Just think of the group as if it was a screen and act accordingly because in the lower level they are essentially the same thing; a container. \$\endgroup\$ – Vacster Oct 8 '16 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.