Absolute and relative position

Hi there

It would be really awesome to have that couple of variables mentioned above. When changing a display object from a group to another, its absolute position may vary if both groups aren’t at the same position. I’ve created a function that returns the global position of an object, but it would be nice if Corona provides it

Hi @roger23,

I think you’re referring to the “localToContent” function? This one is very convenient to get that “content space” point, relative to an object, even if the object is rotated or resides in another group.

http://docs.coronalabs.com/api/type/DisplayObject/localToContent.html

Best regards,

Brent

Hi Brent,

When you create an image at the 50, 50, but then insert it into a group located at 10, 10, that object gets relocated at 60, 60 — and I think there should be an optional parameter to indicate if you want that relocation or maintain its current absolute position. To do this properly and maintain that image at 50, 50 we should use that contentToLocal, but if I want insert this same image into another group at 20, 20, I’ve to call the localToContent and then contentToLocal, right?

If there’s a better performance it would be nice to hear

Hi @roger23,

Your idea is probably the best, using those functions. Or, you could program a little helper function like this:

[lua]

local firstGroup = display.newGroup()

local secondGroup = display.newGroup()

secondGroup.x = -40

secondGroup.y = 200

local function zIndexMoveInPlace( object, prevGroup, newGroup )

    newGroup:insert( object )

    object.x = object.x+(prevGroup.x-newGroup.x)

    object.y = object.y+(prevGroup.y-newGroup.y)

end

local myObject = display.newRect( 120,120,40,40 )

timer.performWithDelay( 1000, function() zIndexMoveInPlace( myObject, firstGroup, secondGroup ) end )

[/lua]

Ok, thank you Brent. I’ll maintain my current function, because it’s probable that my object is in a group inside another group, etc.

Hi @roger23,

I think you’re referring to the “localToContent” function? This one is very convenient to get that “content space” point, relative to an object, even if the object is rotated or resides in another group.

http://docs.coronalabs.com/api/type/DisplayObject/localToContent.html

Best regards,

Brent

Hi Brent,

When you create an image at the 50, 50, but then insert it into a group located at 10, 10, that object gets relocated at 60, 60 — and I think there should be an optional parameter to indicate if you want that relocation or maintain its current absolute position. To do this properly and maintain that image at 50, 50 we should use that contentToLocal, but if I want insert this same image into another group at 20, 20, I’ve to call the localToContent and then contentToLocal, right?

If there’s a better performance it would be nice to hear

Hi @roger23,

Your idea is probably the best, using those functions. Or, you could program a little helper function like this:

[lua]

local firstGroup = display.newGroup()

local secondGroup = display.newGroup()

secondGroup.x = -40

secondGroup.y = 200

local function zIndexMoveInPlace( object, prevGroup, newGroup )

    newGroup:insert( object )

    object.x = object.x+(prevGroup.x-newGroup.x)

    object.y = object.y+(prevGroup.y-newGroup.y)

end

local myObject = display.newRect( 120,120,40,40 )

timer.performWithDelay( 1000, function() zIndexMoveInPlace( myObject, firstGroup, secondGroup ) end )

[/lua]

Ok, thank you Brent. I’ll maintain my current function, because it’s probable that my object is in a group inside another group, etc.