Hi all.
I’m making an endless runner. The ground is made up of MANY ‘tiles’ (all the same). I would like to apply a collision event that operates at a single-tile-level, ie; It drops off the screen when the ‘ball’ bounces on it… I’m struggling to apply events to the tile where the collision happens.
I managed to find a few tutorials that handle event.target, but from what I can gather this is a touch-based trigger, not a collision.
Also, there’s a strong possibility I’ll spawn a LOT of tiles, so indexing each (via a table), is not viable.
Hopefully this code will explain a little better what I’m trying to achieve:
for j = 1,21 do tile = display.newRect( display.contentCenterX, 0, 50, 50) tile.x = 20 + (j\*50) tile.y = 469 tile:setFillColor( 1, 1, 1 ) tile.strokeWidth = 1 tile:setStrokeColor( 0, 0, 0 ) physics.addBody(tile, "static",{ density=2, friction=0.3, bounce=0.0 } ) tile.collision = bounceEventGround tile:addEventListener( "collision", tile ) local groundGroup = display.newGroup() groundGroup:insert(tile) end
And the collision handler looks something like this:
local function bounceEventGround(event) if event.phase == "began" then event.target:setFillColor( 0.2, 0.1, 0.1 ) end return true end
Obviously event.target doesn’t work… is there a kind of equivalent?
All the best