Hi,
is there a more _native_, direct and faster way to calculate an object's global position rather than going through the object's graph via getParent() an sum up positions? It works, but doesn't _feel_ right. Have I overlooked something? Some object.globalX or the like?
Thanks a lot in advance
Sebastian
Comments
But it doesn't work as expected, because results are not the same as if done manually ^^. Does localToGlobal go through the whole graph? It stops at the parent right? I guess I just don't get how localToGlobal works =| .
So the local coordinates are how children positioned relatively to the parent
And global coordinates are how children positioned relatively to the stage
So if your sprite is added to the stage directly, then the results should be the same.
here is a little test:
and it usually worked as expected for me
so what do you have so specific that it won't work?
Thanks a lot for your answer.
stage
-grandparent
--parent
---child
to get the global position of child should we ask :
-> a) parent:localToGlobal(child:getPosition())
or
-> b) stage:localToGlobal(child:getPosition())
In your little test a) you do it on the direct parent and in that case how to know if parent is at the top of the hierarchy?
but in @seppsepp 's example b) it's done on the stage
I did not understand what you explained in your last answer @seppsepp sorry ^^
I've always struggled with localToGlobal()
b) would return back same position as provided, because stage is global and and if you give coordinates that are relative to the stage, they are also the same as global
Unless of course you changed position of stage, which is possible but not recommended as it again complicate stuff a lot
I actually don't know if I made it clearer or worse?
...somethings like that .
// actually don't know if I made it clearer or worse?
weeeeeell
so I now understand that :
1)
stage
-grandparent
--parent
---child
-> child:getParent():localToGlobal(child:getPosition())
and
stage
-grandparent
--parent
---child
(...)
(...)
----------- grandGrandChild
2) grandGrandChild:getParent():localToGlobal(grandGrandChild:getPosition())
those would then be the two correct ways to get the global coordinates for child and grandGrandChild if I understand well.
yep, that is correct