How to find the last object in display group

Let say there is 4 objects in a display group.

local display\_Group = display.newGroup() local rect1 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect2 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect3 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect4 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120)

How could I find the last object in the display group.

i dont get why you keep asking if code, you are providing, would work mate

makes your posts pointless

The ‘numChildren’ field of display groups tells you the number of objects stored in the group.  The last in the list is the last added and the last rendered.

https://docs.coronalabs.com/api/type/GroupObject/numChildren.html

local display\_Group = display.newGroup() local rect1 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect2 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect3 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect4 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) -- rect4 is the last object in the group, thus this will print true: -- print( rect4 == display\_Group[display\_Group.numChildren]) 

The docs here are great, but if you’re not sure where to start, always begin with the API docs for a question about display objects, library functions, methods, etc.

Lol yeah, I will remove in later posts.

Thanks but how do I find the last object in a display object if I do not know the what the last one is.

loop through them checking for the true condition like roamer stated, until you get a hit (true) and you have found it

local lastChild = display\_Group[display\_Group.numChildren]

I told you…    numChildren  is a count of objects in the group

You index the group like you do table

local group = display.newGroup() print( group.numChildren ) display.newCircle( group, 10, 10, 10 ) print( group.numChildren ) -- Use transition to move the circle transition.to( group[1], { x = 200 } )

that comparison was just to elaborate, not the method you would use to find the last object.

I have the STRONG feeling you did not don’t think you read the docs I linked.

Try running this code to understand what I mean and how this works:

local group = display.newGroup() print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect1 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect1.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect2 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect2.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect3 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect3.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect4 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect4.coins = 10 -- Hint! This will print: -- objects in 'group' == 4 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) --To prove that 'rect4' is the last object do this: print( "Num coins: ", rect4.coins ) print( "Num coins: ", group[4].coins ) print( "Num coins: ", group[group.numChildren].coins )

Oh no I read the guides which you provided, I just got confused when you included rect4==. The code I gave was just an example of my issue(I probably should have included that before).

i dont get why you keep asking if code, you are providing, would work mate

makes your posts pointless

The ‘numChildren’ field of display groups tells you the number of objects stored in the group.  The last in the list is the last added and the last rendered.

https://docs.coronalabs.com/api/type/GroupObject/numChildren.html

local display\_Group = display.newGroup() local rect1 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect2 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect3 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) local rect4 = display.newRect(display\_Group, display.contentCenterX, display.contentCenterY, 60,120) -- rect4 is the last object in the group, thus this will print true: -- print( rect4 == display\_Group[display\_Group.numChildren]) 

The docs here are great, but if you’re not sure where to start, always begin with the API docs for a question about display objects, library functions, methods, etc.

Lol yeah, I will remove in later posts.

Thanks but how do I find the last object in a display object if I do not know the what the last one is.

loop through them checking for the true condition like roamer stated, until you get a hit (true) and you have found it

local lastChild = display\_Group[display\_Group.numChildren]

I told you…    numChildren  is a count of objects in the group

You index the group like you do table

local group = display.newGroup() print( group.numChildren ) display.newCircle( group, 10, 10, 10 ) print( group.numChildren ) -- Use transition to move the circle transition.to( group[1], { x = 200 } )

that comparison was just to elaborate, not the method you would use to find the last object.

I have the STRONG feeling you did not don’t think you read the docs I linked.

Try running this code to understand what I mean and how this works:

local group = display.newGroup() print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect1 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect1.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect2 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect2.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect3 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect3.coins = 1 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) local rect4 = display.newRect(group, display.contentCenterX, display.contentCenterY, 60,120) rect4.coins = 10 -- Hint! This will print: -- objects in 'group' == 4 print("---- objects in 'group' == " .. tostring( group.numChildren ) ) --To prove that 'rect4' is the last object do this: print( "Num coins: ", rect4.coins ) print( "Num coins: ", group[4].coins ) print( "Num coins: ", group[group.numChildren].coins )

Oh no I read the guides which you provided, I just got confused when you included rect4==. The code I gave was just an example of my issue(I probably should have included that before).