Android – how to pause and resume in libgdx using the back key?

I'm using libgdx to create a game. Now I have two questions

First, I tried to grab the key so that the game was suspended. I have called the Gdx.input.setCatchBackKey (true) method in my Game class.

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 method of setting up a game using gamescreen, which implements screen and inputprocessor

In gamescreen, I have called setinputprocessor. The code of gamescreen is:

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 if the back button on my Android device is pressed, it will call the Keydown method and call the method pausegame

However, this did not happen. My game was quitting and the Keydown method was not called (if the method Keydown was called, I had tried to log a message, but the message would never appear)

The second problem I encounter is the pause game and pause () method. I think if the home button or device receives a call, it will call the method in gamescreen to pause. So, I think if I want to pause my game when I press the home button, I will call the method pauseGame. in the method pause. It works well. But the problem is that I press the back button to quit the game. After the game is out, I try to start it again, and my texture is not loaded.

By the way, instead of using assetmanager at present, I call a method to load resources in the constructor

resolvent:

My understanding is that you need to set your inputprocessor before calling setcatchbackkey. You can change the gamescreen constructor to:

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

Related questions: in libgdx, how do I get input from the back button?

As for textures, they are not loaded. Make sure that you are unloading all textures and processing them when the game is destroyed (processing function). When you re create the game, be sure to load them again

All categories listed here need to be handled manually, otherwise it will lead to leakage. With regard to textures, you may encounter the problem of loading textures without dealing with the old version first

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>