Search code examples
javaandroidlibgdxbox2dgame-physics

Moving of Actor LibGDX


Please, help me with my issue. I need to move an Actor to special coordinates. I use Stage, so what can I do to solve my problem.

If I'd use OrthographicCamera and rectangle, I'd wrote something like that:

Rectangle myRect = .........
while (myRect.y >= 10) {
myRect.y -= 200 * Gdx.graphics.getDeltaTime();
}

I want to use something like this with Actor. Please, help me. Thank everybody.


Solution

  • LibGDX provides a bunch of Actions that you can perform on Actors. For moving an actor to a specific location you can use MoveToAction. With this you can set the final location and also how long it it takes to move to that location.

    https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/actions/MoveToAction.html

    MoveToAction action = new MoveToAction();
    action.setY(10); // y-position to move to
    action.setDuration(duration); // time (in seconds) to move there
    
    actor.addAction(action);