Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pause and resume in LibGDX using the back key?
    primarykey
    data
    text
    <p>I'm creating a game using LibGDX. Now I have two problems.</p> <p>First, I'm trying to catch back key so that the game will pause. I'm already calling the <code>Gdx.input.setCatchBackKey(true)</code> method in my <code>Game</code> class. Here is the code :</p> <pre><code>public class CvSGame extends Game { public Preferences prefs; @Override public void create() { Gdx.input.setCatchBackKey(true); prefs = Gdx.app.getPreferences("game_pref"); //setScreen(new SplashScreen(this)); //setScreen(new HomeScreen(this)); //setScreen(new GameScreen(this)); GamePlay.initialized(this); } } </code></pre> <p><code>GamePlay.initialized</code> is a method to set the <code>Game</code> with a <code>GameScreen</code> which implements <code>Screen</code> and <code>InputProcessor</code>.</p> <p>In the <code>GameScreen</code>, I already call <code>setInputProcessor</code>. The code for <code>GameScreen</code> is :</p> <pre><code>public class GameScreen implements Screen, InputProcessor{ CvSGame parent; public GameScreen(CvSGame pParent){ parent = pParent; Gdx.input.setInputProcessor(this); } @Override public void show() { } @Override public void resize(int width, int height) { } @Override public void render(float delta) { } @Override public void hide() { } @Override public void pause() { } @Override public void resume() { } @Override public void dispose() { } @Override public boolean keyDown(int keycode) { if(keycode == Keys.BACK) { pauseGame(); } return false; } @Override public boolean keyUp(int keycode) { return false; } @Override public boolean keyTyped(char character) { return false; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { return false; } @Override public boolean mouseMoved(int screenX, int screenY) { return false; } @Override public boolean scrolled(int amount) { return false; } private void pauseGame(){ GamePlay.gameState = GamePlay.PAUSED; } } </code></pre> <p>I think, if the Back button on my Android device is pressed, it will call the <code>keyDown</code> method and the method <code>pauseGame</code> will be called. </p> <p>But, that's not happening. My game is exiting and the <code>keyDown</code> method is not called (I'm already try to log a message if method keyDown is called, but the message never appear).</p> <p>The second problem I have is caused by pausing game with the method <code>pause()</code>. I think if the home button or the device receives a call, the method <code>pause</code> in <code>GameScreen</code> will be called. So, I think if I want to pause my game when the home button is pressed, I call the method <code>pauseGame</code> in method <code>pause</code>. And it works well. But the problem comes after I press back button so the game will quit. After the game quits and I try to start it again, my texture is not loaded.</p> <p>By the way, currently I'm not using <code>AssetManager</code> but call a method to load assets in the constructor.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload