how do I pause the SpriteAnimation in libGDX with a button to be able to view the current frame when pause button is pressed?

拜拜、爱过 提交于 2019-12-24 10:38:27

问题


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.. 
    }


来源:https://stackoverflow.com/questions/12584171/how-do-i-pause-the-spriteanimation-in-libgdx-with-a-button-to-be-able-to-view-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!