Removing object on collision

Hello devs.

I’m having some problems to remove 2 objects involved in a collision.

The “enemy” object is successfully removed, but the “bullet” object is not.

I tried 2 different approaches, both removed only the “enemy” object:

1 -

-- touch firing bullets local bulletsTable = {} function shoot( event ) if (event.phase == 'began') then local bullet = display.newCircle( 0, 0, 10 ) bullet.x = hero.x + 30; bullet.y = hero.y; physics.addBody( bullet, "dynamic", { radius=10, density=1, friction=0.3, bounce=0, filter=heroCollisionFilter } ) bullet.gravityScale = 0 bullet.isBullet = true bullet.isSensor = false bullet.type = "bullet" bullet:setLinearVelocity( 1500,0 ) bulletsTable[bullet] = bullet bullet.collision = function(self, event) if(event.phase == "began") then bulletsTable[self] = nil -- remove from table display.remove(self) -- destroy object end return true end bullet:addEventListener( "collision", bullet ) end end -- SPAWNING ENEMIES local spawnTable = {} local enemyTimer local function spawnEnemy () local enemy = display.newImage( "enemy3\_idle01.png", 0, 0 ) enemy.x = \_W + 50; enemy.y = \_H \* 0.5; sceneGroup:insert(enemy) physics.addBody( enemy, { density=1, friction=0, bounce=0.3, filter=enemyCollisionFilter} ) enemy.gravityScale = 0 enemy.isSensor = false enemy.type = "enemy" enemy:setLinearVelocity( math.random(50,150) \* -1, 0 ) spawnTable[enemy] = enemy -- Collision handling enemy.collision = function(self, event) if(event.phase == "began" and event.other.type == "bullet") then spawnTable[self] = nil -- remove from table display.remove(self) -- destroy object end return true end enemy:addEventListener( "collision", enemy ) end

2- Here I received a runtime error on line 118 “table index is nil”

-- touch firing bullets local bulletsTable = {} function shoot( event ) if (event.phase == 'began') then local bullet = display.newCircle( 0, 0, 10 ) bullet.x = hero.x + 30; bullet.y = hero.y; physics.addBody( bullet, "dynamic", { radius=10, density=1, friction=0.3, bounce=0, filter=heroCollisionFilter } ) bullet.gravityScale = 0 bullet.isBullet = true bullet.isSensor = false bullet.type = "bullet" bullet:setLinearVelocity( 1500,0 ) bulletsTable[bullet] = bullet end end -- SPAWNING ENEMIES local spawnTable = {} local enemyTimer local function spawnEnemy () local enemy = display.newImage( "enemy3\_idle01.png", 0, 0 ) enemy.x = \_W + 50; enemy.y = \_H \* 0.5; sceneGroup:insert(enemy) physics.addBody( enemy, { density=1, friction=0, bounce=0.3, filter=enemyCollisionFilter} ) enemy.gravityScale = 0 enemy.isSensor = false enemy.type = "enemy" enemy:setLinearVelocity( math.random(50,150) \* -1, 0 ) spawnTable[enemy] = enemy -- Collision handling enemy.collision = function(self, event) if(event.phase == "began" and event.other.type == "bullet") then spawnTable[self] = nil -- remove from table display.remove(self) -- destroy object bulletsTable[other] = nil -- remove bullet from table display.remove(other) -- destroy bullet end return true end enemy:addEventListener( "collision", enemy ) end

How should I handle this issue ?

Thanks in advance!

Hi @Krohling,

It looks like you’re specifying “other” in lines 118 and 119. It should be “event.other”.

Take care,

Brent

That’s it, it’s working now.

Thank you very much Brent!

Hi @Krohling,

It looks like you’re specifying “other” in lines 118 and 119. It should be “event.other”.

Take care,

Brent

That’s it, it’s working now.

Thank you very much Brent!