Question

I am pretty new to libgdx, and I'm trying to use gestures to move a ball across the screen when a flinging motion is made on the screen. I've made this gesture listener:

class MyGestureListener implements GestureListener {
    public boolean fling(float velocityX, float velocityY, int button) {
        if (Math.abs(velocityY) > 0) {
            ballPositionY -= velocityY*Gdx.graphics.getDeltaTime();
        }
        return false;
    }   
}

I initialized a detector for the listener in create() and used the following for the ball image in render():

batch.draw(ballImage, ballPositionX, ballPositionY);

The ball moves on fling in proportion to the velocity of the fling. However, it jumps across the screen from its original position to the final position, but I want it to have continuous motion (i.e. see it move across the screen rather than just jump from one point to another). How would I do this? Is fling appropriate for this task, or would you use something else? I am guessing this might have something to do with the frame rate but am not sure.

Thanks in advance!

Was it helpful?

Solution

You're only updating the ball's position when there is an active fling. You need to apply changes (velocity) to the ball on every frame to get smooth continuous animation. In Libgdx, the render callback is called every frame and is where you should update the state of your objects.

In the "fling" callback you should store the new velocity, and then on each call to your render callback you should change the ball's position based on the velocity and the time that has passed since the last render (the Gdx.graphics.getDeltaTime()).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top