Collision handling

Hello devs.

This is my first post in this forum, altough I have been reading it almost every day haha.

Well, my question is what am I doing wrong on my collision handling ? I’m trying to do it so the “enemy” and the “bullet” are removed from the scene and from the bullets/spawnTable when they collide.

My actual code is supposed to remove at least the “enemy”, but when the collision occur, nothing happens

This is the code, any help is really appreciated.

-- Collision Filters local floorCollisionFilter = {categoryBits=1, maskBits=6} local heroCollisionFilter = {categoryBits=3, maskBits=6} local enemyCollisionFilter = {categoryBits=4, maskBits=3} -- touch firing bullets local bulletsTable = {} function shoot( event ) if (event.phase == 'began') then local bullet = display.newCircle( hero.x, hero.y, 10 ) physics.addBody( bullet, "dynamic", { radius=10, density=5, friction=0.3, bounce=0, filter=heroCollisionFilter } ) bullet.gravityScale = 0 bullet.isBullet = true bullet.isSensor = false bullet.collision = onLocalCollision bullet:addEventListener( "collision", bullet ) bullet:setLinearVelocity( 1500,0 ) bulletsTable[#bulletsTable+1] = bullet end end -- SPAWNING ENEMIES local spawnTable = {} local enemyTimer local function spawnEnemy () local enemy = display.newImage( "enemy3\_idle01.png", 500, 150 ) sceneGroup:insert(enemy) physics.addBody( enemy, { density=1, friction=0, bounce=0.3, filter=enemyCollisionFilter} ) enemy.gravityScale = 0 enemy.isSensor = true enemy.collision = onLocalCollision enemy:addEventListener( "collision", enemy ) transition.moveBy( enemy, { x = -570, time = math.random( 2500,3000 ), transition = easing.linear} ) spawnTable[#spawnTable+1] = enemy end local function spawnBurst( speed, count ) timer.performWithDelay( speed, spawnEnemy , count ) end local function spawnLevel( burstSpeed, burstCount ) timer.performWithDelay( burstSpeed, function() spawnBurst( 200, math.random(1,3)); end, burstCount ) end spawnLevel( 1500, 20) -- Collision handling local function onLocalCollision( self, event ) if event.phase == "began" then for i = #spawnTable, 1, -1 do if event.self == spawnTable[i] then timer.performWithDelay(30, function() spawnTable[i]:removeSelf(); spawnTable[i] = nil; table.remove(spawnTable, i) end) end end end end Runtime:addEventListener( 'touch', shoot )

You have an issue with the scope of your methods.

On line 64, you’re setting

bullet.collision = onLocalCollision

And again on line 81, you’re setting

enemy.collision = onLocalCollision

But you’re not defining onLocalCollision until line 99.

That means you’re effectively setting bullet.collision and enemy.collision to nil , which means your collision method is never called.

Yeah, stupid mistake.

Thank you very much for pointing it.

I’m still getting an error but now I can work it out.

You have an issue with the scope of your methods.

On line 64, you’re setting

bullet.collision = onLocalCollision

And again on line 81, you’re setting

enemy.collision = onLocalCollision

But you’re not defining onLocalCollision until line 99.

That means you’re effectively setting bullet.collision and enemy.collision to nil , which means your collision method is never called.

Yeah, stupid mistake.

Thank you very much for pointing it.

I’m still getting an error but now I can work it out.