paul
paul

Reputation: 402

Libgdx Actor.setBounds() relative to Actor.x and Actor.y and etc

Im having trouble of setting bounding rectangle position thats why i ask this. When I set the bounding rectangle position of an object of type Actor, it seems the coordinates i passed to [setBounds(x,y,with,height)]2 is relative to the x and y of the Actor object. Is this true?

And also, when I use Group.addAction(Actions.MoveTo(x,y,duration)), it seems the Actor object dont update its x and y.Is this also true? If yes, how can i update it? I dont know what to override.

Upvotes: 0

Views: 1203

Answers (1)

Tenfour04
Tenfour04

Reputation: 93581

No, setting bounds updates the x and y of the Actor.

Each actor's x and y can be thought of as their local x and y, but their actual position on screen is the sum of their x and y and the x and y of every ancestor (owning Group).

When you move a Group (which is itself an Actor), you are moving its x and y. This does not modify the x and y of its children, but their on-screen positions change because of the above reason.

So if you want to move an Actor to a specific position on screen without moving any of its ancestors, you should only change its local x and y. Therefore, you need to subtract the sum of all its ancestors' x and y from your target world (aka stage) position.

static final Vector2 TEMP = new Vector2(); //reusable to avoid GC

...

TEMP.set(targetX, targetY); //world(aka stage) coordinates of target position
myActor.stageToLocalCoordinates(TEMP); //subtract ancestor positions to get local target position
myActor.addAction(Actions.moveTo(TEMP.x, TEMP.y, duration));

Upvotes: 1

Related Questions