Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx Actions => gradually move Actor from point A to point B

Tags:

libgdx

I want to gradually animate my Actor. I added this Action to move Actor from point A to point B.

addAction(Actions.sequence(Actions.moveBy(1, 1), Actions.moveTo(posX, posY)));

Also tried this (moveTo in 10 seconds):

addAction(Actions.moveTo(posX, posY, 10)));

But Actor moves too fast. What's wrong?

like image 674
Alf Avatar asked Feb 21 '13 14:02

Alf


2 Answers

The second form:

addAction(Actions.moveTo(posX, posY, 10)));

should move your actor to posX, posY over the course of 10 seconds.

The first form will move the actor 1-step in x and y, and after that completes move the actor immediately to posX, posY. Actions.sequence runs the given actions one after the other, they do not modify each other.

How (and where) are you calling act() on the stage? That is what determines how much to update an Actor in a frame, so if you call it multiple times per frame or pass the wrong value, the actions will pass too quickly.

like image 127
P.T. Avatar answered Nov 25 '22 19:11

P.T.


Just because your answer was top when I searched 'Libgdx Move to Point' I will post a solution here.

Here is a solution, not specifically for Actors:

Define you Vector2 variables in the class, these will be used for object position:

protected Vector2 v2Position;
protected Vector2 v2Velocity;

The position is set in the constructor or wherever else. To get the Velocity of the object and move it to the given point:

public void setVelocity (float toX, float toY) {

// The .set() is setting the distance from the starting position to end position
v2Velocity.set(toX - v2Position.x, toY - v2Position.y);
v2Velocity.nor(); // Normalizes the value to be used

v2Velocity.x *= speed;  // Set speed of the object
v2Velocity.y *= speed;
}

Now just add the Velocity to the Position and the object will move to the point given

@Override public void update() {
    v2Position.add (v2Velocity);    // Update position
}
like image 38
Umz Games Avatar answered Nov 25 '22 21:11

Umz Games