Search code examples
javalibgdx

How to make a similar animation to Flappy Bird in Libgdx?


I'm creating a game like flappy bird where the bird flaps his wings only when the screen is touched, but I'm having a problem activating the animation when the screen is touched.

batch.draw(animation.getKeyFrame(myTimeState, false), x, y); //the myTimeState is 0 to render the 1st frame only.

Then when the screen is touched I do this:

//myTimeStep is just basically a controllable timeState for the animation only

    if(Gdx.input.justTouched){
         myTimeState = timeState;
    }else if(Gdx.input.justTouched == false && animation.isAnimationFinished(timeState)){
         myTimeState = 0;
    }

I don't think the animation is able to play all the frames because myTimeStep become 0 immediately after finishing to touch the screen. Also I don't think this is the right way of doing it, if you guys have better ideas or solution please help. Thanks in advance.


Solution

  • There are probably several ways to achieve this. You'll need to increment your timeState, of course, and also it depends how long your animation is and if you want it to loop.

    If you've created your animation to play only once, and then stop (until the screen is touched again), you could simply set your myTimeState to 0 when the screen is touched, and then increment it every frame. The animation will run through and then "stop" on its own when it reaches the end (as you said no loop). The next time someone touches the screen, your myTimeState is set back to 0 and the animation starts again.