Other ways to get the actual y value of objects in the display group

Here is what my project looks like currently:

When you hold on the up button the 4 boxes will move down and when you hold on the down button, the 4 boxes will go up. When the box pass the non moving rectangle at the side, the console will print “past box [whatever]”.

Here is the code for that:

local function gameLoop() if rectangle.y == box1.y+10 then print("Past box 1") end if rectangle.y == box2.y+10 then print("Past box 2") end if rectangle.y == box3.y+10 then print("Past box 3") end if rectangle.y == box4.y+10 then print("Past box 4") end end

However, I decided to add all the 4 rectangles to a display group so I cant shorten my code and would be easier to set up if I wanted to ad more boxes.

But then the gameloop stopped working and I found out that according to Corona documentation:

the object’s inherent x and y position does not change… This is because Corona manages the position of display objects in relation to their parent group.

and to get the actual x and y coordinates you have to do:

local actualBoxX, actualBoxY = box:localToContent( 0,0 )

However I don’t want to repeat that code for every single box and for every box I want to add later.

How do I modify the gameloop to make this work?

Here is my code:

--Buttons local buttonup = display.newCircle(display.contentCenterX+125,display.contentCenterY+150, 30) local uptext = display.newText("up",buttonup.x,buttonup.y,native.systemFont) uptext:setFillColor(0,0,0) local buttondown = display.newCircle(display.contentCenterX+125,display.contentCenterY+220, 30) local downtext = display.newText("down",buttondown.x,buttondown.y,native.systemFont) downtext:setFillColor(0,0,0) local boxGroup = display.newGroup() --Boxes and Rectangle local box1 = display.newRect(boxGroup,0,0,124,88) box1.x=display.contentCenterX-98 box1.y=display.contentCenterY+219 box1:setFillColor(math.random(),math.random(),math.random()) local box2 = display.newRect(boxGroup,0,0,124,88) box2.x=display.contentCenterX-98 box2.y=display.contentCenterY+109 box2:setFillColor(math.random(),math.random(),math.random()) local box3 = display.newRect(boxGroup,0,0,124,88) box3.x=display.contentCenterX-98 box3.y=display.contentCenterY box3:setFillColor(math.random(),math.random(),math.random()) local box4 = display.newRect(boxGroup,0,0,124,88) box4.x=display.contentCenterX-98 box4.y=display.contentCenterX-30 box4:setFillColor(math.random(),math.random(),math.random()) local rectangle = display.newRect(0,0,60,80) rectangle.x = display.contentCenterX+25 rectangle.y = box1.y+10 rectangle:setFillColor(math.random(),math.random(),math.random()) --Button functions and hold function local function moveup(event) boxGroup.y=boxGroup.y+1 end local function movedown(event) boxGroup.y = boxGroup.y-1 end local function handleEnterFrame( event ) if ( needToup == true ) then moveup() end end local needTodown = false local function handleEnterFrame2( event ) if ( needTodown == true ) then movedown() end end Runtime:addEventListener( "enterFrame", handleEnterFrame ) Runtime:addEventListener( "enterFrame", handleEnterFrame2 ) local function handleupButton( event ) if ( event.phase == "began" ) then -- Fire the weapon needToup = true elseif ( event.phase == "ended" and needToup == true ) then -- Stop firing the weapon needToup = false end return true end local function handledownButton( event ) if ( event.phase == "began" ) then -- Fire the weapon needTodown = true elseif ( event.phase == "ended" and needTodown == true ) then -- Stop firing the weapon needTodown = false end return true end buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) --EventListener/GameLoop local function gameLoop() if rectangle.y == box1.y+10 then print("Past box 1") end if rectangle.y == box2.y+10 then print("Past box 2") end if rectangle.y == box3.y+10 then print("Past box 3") end if rectangle.y == box4.y+10 then print("Past box 4") end end Runtime:addEventListener("enterFrame", gameLoop)

Would this work?  :

local actualBoxX, actualBoxY = boxGroup:localToContent( 0,0 ) local function gameLoop() if rectangle.y == boxGroup[x].y+10 then print("Past box "+boxGroup[x]) end end

Objects in a group are positioned relative to the group and the group relative to the screen.

If you do box1:localToContent( 0,0 ) you will get the position of the box relative to the screen so your existing gameloop just needs minor modification.

Instead of

if rectangle.y == box1.y+10 then print("Past box 1") end

do

local boxX, boxY = box1:localToContent( 0,0 ) if rectangle.y == boxY + 10 then    print("Past box 1") end

Objects in a group are positioned relative to the group and the group relative to the screen.

If you do box1:localToContent( 0,0 ) you will get the position of the box relative to the screen so your existing gameloop just needs minor modification.

Instead of

if rectangle.y == box1.y+10 then print("Past box 1") end

do

local boxX, boxY = box1:localToContent( 0,0 ) if rectangle.y == boxY + 10 then    print("Past box 1") end