LibGDX is textured, disappearing when the home button is pressed

I am developing an Android mobile game using libGDX. I noticed that when I press the Home button and then go back to the game, the player disappears. All other actors attached to the stage exist and when I remove the code that moves the player it is drawn as well. It only disappears when moving. I've done so much debugging and sometimes the position seems to update correctly, but the player is invisible, but sometimes it's just NaN. For example, I tried to save the position and speed in pause mode and provide them to the player in the resume function, but nothing helps.

This is what I am doing in the player update function:

// When these lines are removed, the app works perfectly
velocity.add(0.0f, GRAVITY);
velocity.scl(deltaTime);
position.add(velocity);
velocity.scl(1/deltaTime);

      

It doesn't even help if I recreate the player in the resume function

player2 = new Player(resourceManager.getRegion("player"), new Vector2(320.0f, 350.0f), 300.0f);

      

Finally, I tried to create a completely different player object that is drawn after the home button is pressed. It is visible but not moving:

player2 = new Player(resourceManager.getRegion("player"), new Vector2(320.0f, 350.0f), 300.0f);

      

+3


source to share


3 answers


Closely, I dealt with the same issue today, each texture (actors) remains on screen after resuming, but not the main actor who was the one who moved (the main actor in my case). After debugging the watch, I found that when the game state changes between pausing and resuming the render method (my view looks like this) will get 0 for deltaTime:

 @Override public void render(float deltaTime) {
        update(delta);
        spriteBatch.setProjectionMatrix(cam.combined);
        spriteBatch.begin();
        spriteBatch.draw(background, cam.position.x - (WIDTH / 2), 0, WIDTH, HEIGHT);
....
   spriteBatch.end();}

      

deltaTime is the time elapsed since the last render. there is apparently no time elapsed from pause to resume, hence 0. down the cast update chain. I updated my main actor like this.

 velocity.scl(deltaTime);
 position.add(MOVEMENT * deltaTime, velocity.y);

      



which was passed 0 on the next next render after resuming, hence NaN (Not a Number).

Not sure if there is a better way to get around this anyway or not, but this is how I did it, simply checking for zero deltaTime in my main actor update method and replacing with a very small amount of .001f : (note that in a later update deltaTime will not be zero, and that is if the status is only called once and exactly after resuming)

     public void update(float deltaTime) {
            if (deltaTime <= 0) {
                deltaTime=.001f;
            }
        velocity.scl(deltaTime);
        position.add(MOVEMENT * deltaTime, velocity.y);
...
}

      

+2


source


Oh yes, I had the same problem before.
What I did was in onPause (), which is called when the home button is pressed, I destroyed all resource references.
Then, in onResume () that is called when you returned, I assigned the assets again.



0


source


render()

the method keeps getting called when you hit the home button on Android, so it seems like your player's position keeps updating with gravity, so when you resume the game you can't see the player on screen, so use a flag for the position update code and change the flag in pause()

and resume()

method of the interface ApplicationListener

.

enum GameState{
    PAUSED, RESUMED
}

GameState gameState;

@Override
public void create() {

    gameState=GameState.RESUMED;
    ...
}

@Override
public void render() {

    Gdx.gl.glClearColor(1,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if(gameState==GameState.RESUMED) {
       velocity.add(0.0f, GRAVITY);
       velocity.scl(deltaTime);
       position.add(velocity);
       velocity.scl(1/deltaTime);
    }
}

@Override
public void pause() {
    gameState=GameState.PAUSED;
}

@Override
public void resume() {
    gameState=GameState.RESUMED;
}

      

0


source







All Articles