TransWikia.com

LibGDX changing actors parents is resulting in unwanted (x,y) translation

Game Development Asked by RocketScienceGuy on November 2, 2021

I have a Rolodex of task item actors I want to drag to an agenda tablet. My drag and drop functionality is non-native, but came from modifying a book’s implementation. The problem seems to be when I change the parent of a task item, ‘a’, to the agenda’s parent, ‘other’:

other.getParent().addActor(a); 

When I do so the actor’s coordinates shift. I want the actor to remain in the same spot I dropped it down. It worked as desired previously, possibly because the Rolodex’s parent and the agenda’s parent shared the same, whole-screen dimensions. I’ve since moved away from layout to positioning actors and their groups on the screen with setBounds(). Tracking the (x,y) coordinates of my dragged item I’m getting negative values for x while it remains visible on screen, making me think actor.getX() and actor.getY() return values relative to the parent carousel container class I made, and not the screen/stage nor the agenda’s container.

I found a set of methods off of Actor that looked promising, localToXXXX(Vector2), and experimented with a variety of them before settling on the following I think should work:

            Vector2 snapShot = a.localToActorCoordinates(other.getParent(), new Vector2( a.getX(), a.getY()) );
            other.getParent().addActor(a);   
            a.setBounds(snapShot.x, snapShot.y, a.getWidth(), a.getHeight());

…but it doesn’t. Converting it to stage or screen coordinates with the other method options shifts it even farther away from desired. What am I misunderstanding? How can I change an actor’s parent without the x,y shifting?

One Answer

Adding the dragged actor's parent coordinate values to the actor's values did the trick:

Vector2 snapShot = new Vector2(a.getParent().getX() + a.getX(), a.getParent().getY() + a.getY()) ;
other.getParent().addActor(a);   

        a.setBounds(snapShot.x, snapShot.y, a.getWidth(), a.getHeight());

Answered by RocketScienceGuy on November 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP