Reputation: 767
I was developing a game using the Libgdx game library. I came up with this problem while trying to animate from one corner point to touch point using libgdx. Used this tutorial for understanding basics of Libgdx animation basics Link.
I am not able to find how animations in libgdx works,Normal movement of frames takes place but how to start single time normal animation upon touch on screen. Thanks in advance for help.
Edit: My class implements Screen This is what i tried
In class default constructor
animation=new Animation(1/15f, atlas1.getRegions());
In Render method :- To check touch
public void touched()
{
if(Gdx.input.isTouched())
{
touched = true;
x = Gdx.input.getX();
y = Gdx.input.getY();
velocityX = (x - animationX) / 100;
velocityY = (y - animationY) / 100;
}
}
After calling touch method For animation
public void anim()
{
if (touched)
{
elapsedTime += Gdx.graphics.getDeltaTime();
//animationX += velocityX;
// animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
animfinished=animation.isAnimationFinished(elapsedTime);
}
if(touched)
{
batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
}
}
Upvotes: 1
Views: 958
Reputation: 1
public void anim()
{
if (touched)
{
elapsedTime += Gdx.graphics.getDeltaTime();
//animationX += velocityX;
// animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
animfinished=animation.isAnimationFinished(elapsedTime);
}
if(touched)
{
batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
}
}
Upvotes: 0
Reputation: 7364
You can use InputListener for touch event and use this to control your animation.
Below is simple approach. Of course, more proper would be to extend Animation class and move there all animation logic.
public class AnimationTest implements ApplicationListener, InputListener {
boolean touched = false;
Animation animation;
float elapsedTime = 0;
float touchedX, touchedY;
float animationX, animationY;
float velocityX, velocityY;
// ... do not forget to register InputListener here ...
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer,int button) {
touched = true;
touchedX = x;
touchedY = y;
velocityX = (touchedX - animationX) / 100;
velocityY = (touchedY - animationY) / 100;
}
// ...
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
if (touched) {
elapsedTime += Gdx.graphics.getDeltaTime();
animationX += velocityX;
animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, true), animationX, animationY);
}
batch.end();
}
}
This is where animation occurs:
batch.draw(animation.getKeyFrame(elapsedTime, true), animationX , animationY);
You just gets appropiate keyframe from your animation and draws this on batch.
Upvotes: 3