Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx: phone's back button continues between screen

Tags:

java

input

libgdx

I have three classes that implement LibGdx Screen class:

MainMenuScreen, LevelSelectScreen, GameScreen

For now I have MainMenuScreen's setCatchBackKey set to false, so the game will just exit on back press.

LevelSelectScreen and GameScreen have setCatchBackKey set to true.

Within the GameScreen's render function I have:

@Override
public void render(float delta)
{
    gameTime = delta;

    if (Gdx.input.isKeyPressed(Keys.BACK))
       new LevelSelectScreen(game);

    ...
}

which sets the screen to show level select and in LevelSelectScreens's render function I have:

@Override
public void render(float delta)
{
    if (Gdx.input.isKeyPressed(Keys.BACK))
        new MainMenuScreen(game);

    Update();
    Draw();
} //end render

The problem I run into with this is that when I press back on the GameScreen the LevelSelectScreen flashes to the screen for a moment and then straight back to the MainMenuScreen.

I figure that I could set some variable to check whether Back is already being pushed when the screen initializes, but was just curious if there is some "magical" function already included within LibGdx or just code structure that would also work around this problem.

like image 813
Fleck Avatar asked Apr 27 '12 19:04

Fleck


2 Answers

First of all extend the Stage class like below:

public static class MyStage extends Stage{
    public MyStage(float width, float height, boolean keepAspectRatio, SpriteBatch batch){
        super(width, height, keepAspectRatio, batch);
    }       
    @Override
    public boolean keyDown(int keyCode) {
        if(keyCode==Keys.BACK||keyCode==Keys.MENU){
            if(getHardKeyListener()!=null)
                getHardKeyListener().onHardKey(keyCode, 1);
        }           
        return super.keyDown(keyCode);
    }
    @Override
    public boolean keyUp(int keyCode) {
        if(keyCode==Keys.BACK||keyCode==Keys.MENU){
            if(getHardKeyListener()!=null)
                getHardKeyListener().onHardKey(keyCode, 0);
        }
        return super.keyUp(keyCode);
    }   

    /*********************Hard key listener***********************/
    public interface OnHardKeyListener{
        /**
         * Happens when user press hard key 
         * @param keyCode Back or Menu key (keyCode one of the constants in Input.Keys)
         * @param state 1 - key down, 0 - key up  
         */
        public abstract void onHardKey(int keyCode, int state);
    }
    private OnHardKeyListener _HardKeyListener = null;  
    public void setHardKeyListener(OnHardKeyListener HardKeyListener) {
        _HardKeyListener = HardKeyListener;
    }       
    public OnHardKeyListener getHardKeyListener() {
        return _HardKeyListener;
    }
}

Then in your Screen class:

protected final MyStage stage;
public YourScreen(Game game)
{
    this.game = game;   
    stage = new MyStage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true, game.spriteBatch);                    

    Gdx.input.setInputProcessor(stage);  
    //prevents the app from being pause...      
    Gdx.input.setCatchBackKey(true);
    Gdx.input.setCatchMenuKey(true);

    //Sets hard key listener...
    stage.setHardKeyListener(new OnHardKeyListener() {          
        @Override
        public void onHardKey(int keyCode, int state) {
            if(keyCode==Keys.BACK && state==1){
                //do something      
            }       
        }
    });
}

Hope it helps you or somebody else who is looking for this stuff.

like image 126
Nolesh Avatar answered Nov 12 '22 23:11

Nolesh


Instead of using Gdx.input.isKeyPressed(int) you can implement the InputProcessor interface.

Documentation for InputProcessor: http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/InputProcessor.html

Dont forget to register your InputProcessor for it to receive input events, with: Gdx.input.setInputProcessor(InputProcessor);

For this particular case use keyDown.

like image 33
Kflexior Avatar answered Nov 12 '22 23:11

Kflexior