0
\$\begingroup\$

I have a screen with multiple stages (pre-game, game, pause) and on the game stage I have a pause button. This is implemented as:

public class PauseButton extends Actor {
    private NumberzGame m_game;
    private TextureRegion m_pauseButton;
    private int m_buttonSize;

    public PauseButton(NumberzGame game, Stage stage, int size) {
        super();

        m_game = game;
        m_buttonSize = size;
        m_pauseButton = createPauseButton();

        setWidth(m_buttonSize);
        setHeight(m_buttonSize);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(m_pauseButton, getX(), getY());

        super.draw(batch, parentAlpha);
    }

    private TextureRegion createPauseButton() {
        Pixmap map = new Pixmap(m_buttonSize, m_buttonSize, Format.RGBA8888);
        map.setColor(ColorUtils.colorFromHex(NumberzGame.COLOR_BACKGROUND));
        map.fill();

        int radius = (m_buttonSize / 2) - 4;
        map.setColor(ColorUtils.colorFromHex(NumberzGame.COLOR_SOUND_BUTTON_BACKGROUND));
        map.fillCircle(m_buttonSize / 2, m_buttonSize / 2, radius);
        map.setColor(ColorUtils.colorFromHex(NumberzGame.COLOR_SOUND_BUTTON));

        int width = 2 * radius - 10;
        int height = width;
        int xpos = (m_buttonSize - width) / 2;
        int ypos = (m_buttonSize - height) / 2;

        Pixmap pause = m_game.getAssets().get(NumberzGame.ID_PAUSE_PIXMAP, Pixmap.class);
        map.drawPixmap(pause, 0, 0, pause.getWidth(), pause.getHeight(), xpos, ypos, width, height);

        TextureRegion result = new TextureRegion(new Texture(map));
        map.dispose();

        return result;
    }
}

In the screen class I have the following code to add the pause button to the game stage:

m_pauseButton = new PauseButton(getGame(), m_gameStage, NumberzGame.PLAY_BUTTON_WIDTH);
m_pauseButton.setX(width - NumberzGame.PLAY_BUTTON_WIDTH - NumberzGame.MARGIN);
m_pauseButton.setY(height - NumberzGame.PLAY_BUTTON_WIDTH - NumberzGame.MARGIN);
m_pauseButton.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        m_state = GameState.PAUSED;
        getGame().getWorld().pauseWorld(true);
    };
});
m_gameStage.addActor(m_pauseButton);

When I run the application the first click on the pause button properly pauses the game and after I resume the game, I need to press the pause button twice to have it trigger.

What am I missing here?

\$\endgroup\$
  • \$\begingroup\$ how is your inputprocessor set up? \$\endgroup\$ – wes May 19 '14 at 13:15
  • \$\begingroup\$ I am missing something too :) I assume you also have a "unpause" button, because, just by looking at it, i'll say that this button will pause the game and never unpause it. (Your "clicked" method set the state to paused, but how do you resume the game then ?) Just asking, but why not using built in LibGdx UI buttons ? Personnaly i find them neat and customizable enough :) \$\endgroup\$ – Khopa May 19 '14 at 13:19
  • \$\begingroup\$ The input processor is set to the current stage depending on the game state. This is working fine as I have proper input handling for each of the stages. \$\endgroup\$ – Kees de Bruin May 20 '14 at 7:18
  • \$\begingroup\$ The pause button will trigger a PAUSED game state and this will switch to a different stage where the user can either resume (and thus unpause the game) of quit the current game. \$\endgroup\$ – Kees de Bruin May 20 '14 at 7:19
  • \$\begingroup\$ So, you push the pause button in the GameStage, it works fine and switch to the PauseStage. Then you push the resume button, and it works fine and send you back to the GameStage. But now, when you try to push the pause button, you have to push it twice to pause the game again. Am i correct ? \$\endgroup\$ – Khopa May 20 '14 at 7:58
1
\$\begingroup\$

First thing: I see that you are using a stage from scene2D. A better way to go about this pause button is to use the scene2Dui class TextButton. You can customize it with a pixmap to make it look pretty much the same.

As for your current problem, I am speculating that the problem might be that the ClickListener doesn't detect the mouse release because you are switching screens simultaneously. Try using a ChangeListener.

\$\endgroup\$
  • \$\begingroup\$ The problem was indeed caused by the switching of stages in the game screen. I've made some design changes to the application so the game screen only has a single stage and no more problems. \$\endgroup\$ – Kees de Bruin Dec 12 '14 at 10:44

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.