how do I pause the SpriteAnimation in libGDX with

2019-08-08 17:13发布

问题:

I was trying to pause the animation with a pause/resume button, by doing this :

public static int gameStatus = 1;
public int gamePaused = 0;
public int gameRunning = 0;
public void create(){
  gameRunning = 1;
  //.. code
}
public void render(){
   if(gameStatus == gameRunning){ 

//some code .. 

            pause.setClickListener( new ClickListener() {

                @Override
                public void click(Actor actor, float x, float y) {
                    System.out.println("pause is pressed");
                    //to stop the animation at a particular frame
                    spriteBatch.begin();
                    spriteBatch.draw(currentFrame, 100, 100);                        
                    spriteBatch.end();
                    if(gameStatus!=gamePaused)
                    {
                        gameStatus = gamePaused;
                    }
                    else
                    {
                        gameStatus = 1;
                    }

                }
            });
//rest of my render animation code .. 
   }
}

But on the desktop it shows 3/4th of the current frame and 1/4th of the next frame. Any idea why? And in android it behaves like the entire image is shivering.I'm wondering maybe its because its calling render() again and again. I dont understand why there is a difference in the ouput in android and the desktop version. Aren't they supposed to be the same? I think there is some indexing issue between the two. Any body else experienced the same issue? Any suggestions or a proper way to pause the animation or a tutorial link would be really helpful. Thankyou.

回答1:

I did the pause implementation without any flickering issues like I had by simply removing the spriteBatch.draw() from the pause method, and by using a boolean variable to know when to render and when not to render inorder to pause the animation. This is how I did it (in case anyone had the same issues):

private boolean isPlaying = true;

    @Override
    public void pause() { 
    Gdx.app.log("pause():", "pausing the app");
        if( isPlaying )
        {
            isPlaying = false;
        }
        else
        {
            isPlaying = true;
        }


    }

    @Override
    public void render() {      

    Gdx.app.log("123123", (!seekBar.isDragging())+"");
    if( isPlaying )
              // .. do the animation.. 
    }