Groups and coordinates

Hi guys!

I’ve got this code

local group0=display.newGroup()  
local img=display.newImage("foo.png")  
group0:insert(img)  
print("img X: "..img.x)  
print("group X: "..group0.x)  

I don’t understnad why the x coordinate of the image is different that the one of the group (containing only that image)
This cause me a problem in the code i’m trying to develop, cause sometimes I work with images and sometimes with images in groups.

I appreciate any help!

Thanks in advance! [import]uid: 44013 topic_id: 8862 reply_id: 308862[/import]

Always remember, every object you create is always placed in a group. When you don’t insert an object into a specific group, the object is placed in the current stage group.

The x/y attributes of an object tell you where that object is within its parent group. Group’s default to 0:0, which is why by default, when you create an object the x/y values correspond to their exact position on your screen. When you modify the parent group’s x/y values, the child object’s position on the screen is offset by that same amount.

To demonstrate what I’m talking about, print the x/y values of both img and group0 before you insert it into the group.

Normally, you’d modify the x/y value of the objects themselves, but if you want to want to move all objects within a group, you’d then modify the x/y values of the entire group. [import]uid: 52430 topic_id: 8862 reply_id: 32341[/import]

I understand your frustration…the docs are ABYSMAL in explaining this issue:

Here’s the crux of it:

For CERTAIN types of display objects:
(Ansca really needs to document which ones)

the FACTORY functions (those which CREATE the obj), receive params for the TOP & LEFT of the obj being created.

But AFTER the object is created, all FUTURE references to X & Y refer to the reference point of the same obj.

For many of those object types, the reference point defaults to the CENTER of the object.

So you can pass TOP=0 when you create a new 10x10 rectangle, but when you check it’s X value, you will see that it is 5 because that’s the (in-group) position of it’s CENTER. [import]uid: 6175 topic_id: 8862 reply_id: 32481[/import]

Hi guys,

Thanks for tour posts!

Jonathan, I understand what you are talking about, but it seems that what you say it’s no wath i get.
I print the x/y values of both img and group0 before inserting it into the group and I get 36.5 for the x coordinate of the image, not 0 as i think it would be.

I think is what dgaedcke is talking about…

Why I get 36.5 for the x coordinate instead of 0?
Why I get 0 if I don’t insert the image in the group and why 36.5 if I insert it? [import]uid: 44013 topic_id: 8862 reply_id: 32484[/import]