Reputation: 109
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?
Upvotes: 1
Views: 189
Reputation: 995
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.
Upvotes: 1