How to pause and resume work in LibGDX using a return key?

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"); //setScreen(new SplashScreen(this)); //setScreen(new HomeScreen(this)); //setScreen(new GameScreen(this)); GamePlay.initialized(this); } } 

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.

+4
source share
3 answers

As far as I understand, you need to set InputProcessor before calling setCatchBackKey . You can fix this by changing the GameScreen constructor to:

 public GameScreen(CvSGame pParent) { parent = pParent; Gdx.input.setInputProcessor(this); Gdx.input.setCatchBackKey(true); } 

Related question: In libgdx, how do I get input using the back button?

As for texture not downloadable. Make sure that when your game is destroyed (the dispose function), you unload all your textures and delete them. When your game is recreated, be sure to download them again.

All of the classes listed here must be removed manually or may cause leaks. As for textures, you may run into problems loading textures without first deleting your old copies.

+3
source

In your use case (when you call the pause method when you click the button), you don’t need to do anything special to catch the back key. Libgdx will reference the pause method on your registered ApplicationListener . See ApplicationListener.pause() javadoc , which says:

Called when the application is paused. The application is suspended until it is destroyed when the user clicks the "Home" button on Android or an incoming call has occurred. On the desktop, this will be called immediately before calling the utility ().

Game ApplicationListener will automatically move pause to the current Screen , even.

0
source

Do you implement InputProcessor in your class? And also, as @MCeley said you should put this code in order:

  Gdx.input.setInputProcessor(this); Gdx.input.setCatchBackKey(true); 

Also, in the pauseMenu() method add a boolean parameter, the good old flag:

  private void pauseGame(boolean isPaused){ if(isPaused) { GamePlay.gameState = GamePlay.PAUSED; } else { GamePlay.gameState = GamePlay.UNPAUSED; } } 

Add a boolean as a flag and field to the GameScreen class. Remember that a tag is like a marker, and this is the best way to meet or cancel it. Before accepting my answer, you should study the flow of how the sequence works, starting at the end, not at the beginning. You must find out how this happened by crossing the steps. Please note that this is pseudo code, in some way.

0
source

All Articles