Well, we’ve got a few things here.
First, you don’t appear to be setting any variables to identify your coins when they are spawned. The variable you have here:
coin.id = "coin"
Gives the id of “coin” to every object, but that isn’t very individual. Depending on your collision listener, it might not be best practice. In addition, creating objects in this way, while technically feasible, doesn’t give much control over them. Creating them as objects in a table would give you better control over their individual interactions.
Second, and I’m just hypothesizing because I don’t know what your collision listener looks like, when you’re inserting your coins into a display group, and then inserting that display group into another display group, it can be hard to remember which objects are in which group. If you have objects that are outside of the given display group and display groups are over-written, you might not see objects collide in the right way.
Third, and again, this is a hypothesis, if you’re not using physics to have objects collide with each other, you won’t see any collisions happen. Seems obvious, but if you use transition.to instead of body.applyForce (or similar), you aren’t going to get the same results.
Here’s a quick snippet of how I build a tiled background for testing purposes:
local backg = {} local moveY = 10 local moveX = 20 local cellWidth = 34 local cellHeight = 34 local gridGroup = display.newGroup() local function buildBack1() for x = 1, 20 do backg[x] = {} for y = 1, 5 do backg[x][y] = display.newRect(0, 0, cellWidth, cellHeight) backg[x][y].x = x\*cellWidth+moveY backg[x][y].y = y\*cellHeight+moveX backg[x][y].id = ("backg["..x.."]["..y.."]") backg[x][y]:setFillColor(1, 0, 0) gridGroup:insert(backg[x][y]) end end end buildBack1()
Notice how the tiles are display objects, and also part of a table. This way, I can reference them as individual tiles, or as a large group, later on.
Is there a way you could post your collision code so we can confirm how and what is supposed to be interacting with what?