I am creating a game using LibGDX. Now I have two problems.
First, I'm trying to catch the key so that the game stops. I already call the Gdx.input.setCatchBackKey(true) method in the Game class. Here is the code:
public class CvSGame extends Game { public Preferences prefs; @Override public void create() { Gdx.input.setCatchBackKey(true); prefs = Gdx.app.getPreferences("game_pref");
GamePlay.initialized is a Game installation method with GameScreen that implements Screen and InputProcessor .
In GameScreen I'm already calling setInputProcessor . Code for GameScreen :
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; } }
I think that if I press the back button on my Android device, it will call the keyDown method and the keyDown method will be called.
But this does not happen. My game ends and the keyDown method keyDown not called (I'm already trying to register a message if the keyDown method is called, but the message never appears).
The second problem I encountered was caused by pausing the game using the pause() method. I think that if the home button or device receives a call, the pause method in GameScreen will be called. So, I think that if I want to pause the game when the home button is pressed, I call the pauseGame method in the pause method. And it works well. But the problem arises after I press the "Back" button to stop the game. After the game completes and I try to start it again, my texture does not load.
By the way, I'm not currently using AssetManager , but AssetManager method to load assets in the constructor.