How to know the coordinates of an object in a group?

Hello!
I’m working with 2 images that I want to move. If I insert these images on a group and I move them, I don’t know what are their x,y coordinates.
I print their x,y properties but I’ve got the same values as before moving them.
How can I get the x,y coordinates after moving?

local a=require("move")  
local ui = require("ui")  
  
local width,height=display.contentWidth,display.contentHeight  
local bg=display.newRect(0,0,width,height)  
bg:setFillColor(255,255,255)  
local button\_pressMe  
local button\_coordinates  
local image1, image2  
  
local displayingImage = function (event)  
 if event.phase == "release" then  
 image1=display.newImage("item1.png",100,100)  
 image2=display.newImage("item2.png",160,100)  
 button\_pressMe.isVisible=false  
 button\_coordinates.isVisible=true  
 local group=display.newGroup()  
 group:insert(image1)  
 group:insert(image2)  
 group:addEventListener("touch",a.move)  
 end  
end  
local printingCoordinates = function (event)  
 if event.phase == "release" then  
 print("Item1 x coordinate: "..image1.x)  
 print("Item1 y coordinate: "..image1.y)  
 print("Item2 x coordinate: "..image2.x)  
 print("Item2 y coordinate: "..image2.y)  
 end  
end  
  
button\_pressMe = ui.newButton{  
 default = "buttonGray.png",  
 over = "buttonGrayOver.png",  
 onEvent = displayingImage,  
 text="Press me",  
 font=native.systemFont,  
 size=18,  
 emboss = true  
}  
button\_coordinates = ui.newButton{  
 default = "buttonGray.png",  
 over = "buttonGrayOver.png",  
 onEvent = printingCoordinates,  
 text="Print coordinates",  
 font=native.systemFont,  
 size=18,  
 emboss = true  
}  
  
button\_pressMe.x=width/2  
button\_pressMe.y=height-30  
button\_coordinates.x=width/2  
button\_coordinates.y=height-30  
button\_coordinates.isVisible=false  

Any help is appreciated! [import]uid: 44013 topic_id: 10172 reply_id: 310172[/import]

[lua]local localGroup = display.newGroup()

local img1 = display.newRect(0,0,100,50)
local img2 = display.newRect(100,100,100,50)
localGroup:insert(img1)
localGroup:insert(img2)

local function moveTarget(e)
if “began” == e.phase then
display.getCurrentStage():setFocus(e.target)
e.target.isFocus = true
elseif “moved” == e.phase and e.target.isFocus then
e.target.x = e.x
e.target.y = e.y
elseif “ended” == e.phase and e.target.isFocus then
display.getCurrentStage():setFocus(nil)
e.target.isFocus = false
end
end
img1:addEventListener(“touch”,moveTarget)
img2:addEventListener(“touch”,moveTarget)

local button = display.newText(“tap me”,0,0,nil,50)
button.x = 240
button.y = 260
localGroup:insert(button)
local function callMe()
print(img1.x … " “…img1.y …” “…img2.x …” "…img2.y)
end
button:addEventListener(“tap”,callMe)
return localGroup[/lua] [import]uid: 12482 topic_id: 10172 reply_id: 37147[/import]

Thanks for your reply, hgvyas123!
I understand that you believe is not possible to do it as i’m trying it.
[import]uid: 44013 topic_id: 10172 reply_id: 37148[/import]

don’t know about that way actually i don’t know what is

local a=require(“move”)

so given this code

:slight_smile: [import]uid: 12482 topic_id: 10172 reply_id: 37150[/import]

You are right!, excuse me.
This is the code in move.lua

module(..., package.seeall)  
  
function move (event)  
 local t=event.target  
 local fase=event.phase  
  
 if fase=="began" then  
 display.getCurrentStage():setFocus( t )  
 t.isFocus=true  
 t.x0=event.x - t.x  
 t.y0=event.y - t.y  
 elseif t.isFocus then  
 if fase=="moved" then  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
 elseif fase=="ended" or fase=="cancelled" then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus=false  
 end  
 end  
 return true  
end  

I appreciate so much your help!

[import]uid: 44013 topic_id: 10172 reply_id: 37167[/import]

Could anybody help me with this, please?
How can I get the coordinates (relative to the stage) of the objects placed in a group?

Thanks [import]uid: 44013 topic_id: 10172 reply_id: 39197[/import]

Is this what you’re looking for?

object:localToContent()
_Maps given x and y coordinates (center) in local coordinates to content (or stage) coordinates.

This method is useful for mapping the coordinates of a display object within a group to the content (viewable display screen).

Syntax: x_content, y_content = object:localToContent(x, y)_

http://developer.anscamobile.com/reference/index/objectlocaltocontent

There’s a reverse of this too: object:contentToLocal() [import]uid: 9747 topic_id: 10172 reply_id: 39203[/import]

Thanks for your reply, IgnisDesign!
I think that the function you talk me about is just what I need, but it seems that this function has a bug, so don’t work as it was supposed.

Look at this:

local ui = require("ui")  
local width,height=display.contentWidth,display.contentHeight  
  
local group = display.newGroup() -- Create a group  
   
local rect = display.newRect( 0, 0, 10, 20 ) -- Create a rectangle object  
group:insert( rect ) -- add rect to group  
   
group.x = 100; group.y = 200 -- move the group  
   
rect:setReferencePoint( display.TopLeftReferencePoint )  
   
rect.x = 5  
rect.y = 10  
   
rectContentX, rectContentY = rect:localToContent( 0, 0 )  
groupContentX, groupContentY = group:localToContent( 0, 0 )  
 local printingCoordinates = function (event)  
 if event.phase == "release" then  
 group.x=group.x+50  
 group.y=group.y+50  
 print( "group's position on screen: ", groupContentX, groupContentY ) -- prints 100, 200  
 print( "group.x,y: ", group.x, group.y ) -- prints 100,200  
   
 print( "rect's position on screen: ", rectContentX, rectContentY ) -- prints 110, 220  
 print( "rect.x,y: ", rect.x, rect.y ) -- prints 5, 10  
 end  
end  
  
button\_coordinates = ui.newButton{  
 default = "buttonGray.png",  
 over = "buttonGrayOver.png",  
 onEvent = printingCoordinates,  
 text="Print coordinates",  
 font=native.systemFont,  
 size=18,  
 emboss = true  
}  
button\_coordinates.x=width/2  
button\_coordinates.y=height-30  

I only add a button to change the group coordinates, expecting to get the new coordinates of the rect, but those are always the same, so I suppose the localToContent() function doesn’t work.

What do you think?
Am I doing anything wrong?
[import]uid: 44013 topic_id: 10172 reply_id: 39308[/import]

The function definitely works, it’s just something about how you’re using it that’s not correct.

For one thing, you might need to supply the object’s coordinates to the function, not “0,0”.

instead of: rectContentX, rectContentY = rect:localToContent( 0, 0 )
try this: rectContentX, rectContentY = rect:localToContent( rect.x, rect.y )

Also, when you move the group, you probably need to re-call this function again to get the NEW coordinates.

I suggest just tinkering around, supplying different values, and something will click. I use the reverse of this (ContentToLocal) in my game. It was tricky at first but these functions work great when you need them. :slight_smile:

Hope this helps!
Brent
[import]uid: 9747 topic_id: 10172 reply_id: 39359[/import]

Hello,

Thanks for your reply, IgnisDesign!
Tom, from Ansca, say this:

The function is working fine but there is a flaw in your code. You are calling localToContent() before you move the group so it shows the position before the move.

I only change the order of the calling and now is working fine.

[import]uid: 44013 topic_id: 10172 reply_id: 39596[/import]