bad argument #-2 to 'insert' (Proxy expected, got nil)

local group = display.newGroup( ) local image = display.newRect( 100, 100, 100, 100 ) group:insert( image ) for i=group.numChildren, 1, -1 do display.remove( group[i] ) group[i] = nil end function generate() group:insert( image ) end -- timer.performWithDelay( 1000, generate ) -- bad argument #-2 to 'insert' (Proxy expected, got nil) generate() -- work well

Can anyone please explain why the “timer.performWithDelay” code is wrong?

How can I insert the image into the group after a time I want?

You create image, insert it into group, then immediately destroy it as part of the numChildren loop. Image is now pointing to nothing. It is a fluke of timing that it works without the delay, with a delay image is no longer a display object.

You create image, insert it into group, then immediately destroy it as part of the numChildren loop. Image is now pointing to nothing. It is a fluke of timing that it works without the delay, with a delay image is no longer a display object.