简体   繁体   中英

LibGDX Scene2D: How to apply actions to actors which are not located at bottom left corner?

Here is the code from the Main class:

MyActor myActor = new MyActor;
moveAction = new MoveToAction();
moveAction.setPosition(600f, 750f);
myActor.addAction(moveAction);

And here is the code from the MyActor class

@Override
public void draw(Batch batch, float alpha){
    batch.draw(texture,getX(),getY());
}

If it is written like this, the action will work but the texture starting position is in the bottom left corner and if I replace getX() and getY() with other coordinates, the actions will not work and the texture will just stay at the same position. So how exactly I set starting position for the actor?

Actors have their own position values. You probably know this considering you use them to draw their texture at the correct location. Therefore, what you need to do is set the Actor's initial position when you create it. Something like this:

MyActor myActor = new MyActor;
myActor.setPosition(100, 100);

Now, if you want to give the actor an action to move somewhere else, instead of creating a new MoveToAction , use the Actions convenience methods like this:

myActor.addAction(Actions.moveTo(600, 750));

This will move the Actor to that position instantaneously, so if you want the Actor to move there in a certain period of time you would have to write that line like this:

myActor.addAction(Actions.moveTo(600, 750, duration));

duration is a float that holds the number of seconds you want the actor to take to get to that specified location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM