Reputation: 75
I'm trying to use the back key on the android device to setscreens for my game..But it does'nt seem to work.. I read few posts online n did this...
In the menuscreen if the backkey is pressed the app should exit..But no action is triggered...Even if i press the back key numerous no action is triggered...
public class MenuScreen extends AbstractScreen implements InputProcessor {
@Override
public void show()
{
Gdx.input.setInputProcessor(this);
Gdx.input.setCatchBackKey(true);
Gdx.input.setCatchMenuKey(true);
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
if(keycode == Keys.BACK)
{
Gdx.app.exit();
}
return false;
}
In LevelScreen when the back key is pressed the screen should set to menuscreen , but my application will quit Is there way to disable the back key ??Nothing should happen if its pressed ?
public class LevelScreen extends AbstractScreen implements InputProcessor {
@Override
public void show()
{
Gdx.input.setInputProcessor(this);
Gdx.input.setCatchBackKey(true);
Gdx.input.setCatchMenuKey(true);
}
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.BACK)
{
game.setScreen(game.geMenuScreen());
}
// TODO Auto-generated method stub
return false;
}
Upvotes: 3
Views: 2558
Reputation: 21087
You should @Override keyUp button instead.
@Override
public boolean keyUp(int keycode) {
if ((state == SETTING_STATE || state == TOPSCORES_STATE) && keycode == Keys.BACK) {
state = RUNNING_STATE;
is_game_start = true;
} else {
Gdx.app.exit();
}
return false;
}
Upvotes: 2
Reputation: 4529
Firstly, you probably want to change it from using keyDown() to keyUp() in the input processor. keyUp() will only be called once when the user releases the key, but keyDown() will be called every frame the back key was down (roughly 60 times a second). So your app could switch screens and close on you in 2 frames which likely isn't what you want.
This is likely why you see the app close from the level screen. It will be going LevelScreen -> ScoreScreen -> MenuScreen and then quitting in 3 frames (roughly 50 milliseconds). Here is some sample code of an application which starts in a ScoreScreen. When the user presses (and releases) the back button goes to the menu screen. Next, when they press (and release) the back button again it then closes the application.
You will need to adapt this code to your game, but this should give you something to look at and work with. (Note: I have verified this working on Android 2.3.3 and LibGDX 0.9.8)
public class BackTest extends Game {
public Screen menu;
public Screen score;
@Override public void create() {
Gdx.input.setCatchBackKey(true);
this.menu = new MenuScreen(this);
this.score = new ScoreScreen(this);
this.setScreen(this.score);
}
public static abstract class AbstractScreen implements Screen {
@Override public void render(final float delta) {}
@Override public void resize(final int width, final int height) {}
@Override public void show() {}
@Override public void hide() {}
@Override public void pause() {}
@Override public void resume() {}
@Override public void dispose() {}
}
public static class MenuScreen extends AbstractScreen {
public final BackTest game;
public MenuScreen(final BackTest game) {
this.game = game;
}
@Override public void show() {
Gdx.app.log("Menu", "Show");
Gdx.input.setInputProcessor(new InputAdapter() {
@Override public boolean keyUp(final int keycode) {
if (keycode == Keys.BACK) {
Gdx.app.log("Menu", "Quit");
Gdx.app.exit();
}
return false;
}
});
}
}
public static class ScoreScreen extends AbstractScreen {
public final BackTest game;
public ScoreScreen(final BackTest game) {
this.game = game;
}
@Override public void show() {
Gdx.app.log("Score", "Show");
Gdx.input.setInputProcessor(new InputAdapter() {
@Override public boolean keyUp(final int keycode) {
if (keycode == Keys.BACK) {
Gdx.app.log("Score", "Back");
ScoreScreen.this.game.setScreen(ScoreScreen.this.game.menu);
}
return false;
}
});
}
}
}
Upvotes: 3