Trying to remove Object on collision

Hey guys I can not seem to figure out how to make it so that when my “enemy” collides with the “boat” it will destroy its self and print(“you lose”) I have other objects inside the spawn function and I am destroying the same object but it won’t work when I try to get it to collide with the boat.

Any Help Thanks,Tyler J.

local boat = display.newRect(0,99,30,400) physics.addBody(boat,"static") boat.isSensor= true local function spawnSmiles() for i=1,numberSmiles do local zombie1 = display.newImage("zombie.png",430,50, 45, 45); -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default physics.addBody(zombie1,"dynamic") transition.to( zombie1, { time=math.random(2000,8000), x=-10, y=50} ); -- this is the code that won't work below this function collisionwboat1(self, event ) zombie1:removeSelf() end boat:addEventListener("collision",collisionwboat1) if cannon1.x\<zombie1.x then if bullets1 \>= 1 then print("shoot") local bullet1 = display.newRect(cannon1.x,cannon1.y,30,10) transition.to(bullet1, { time=9000, x=zombie1.x, y=zombie1.y} ); physics.addBody(bullet1,"static") bullets1 = bullets1-1 moneydis.text = money if bullets1\<=2 then local reloadtxt= display.newText("Reload Cannon 1",300,50,native.systemFont,25) reloadtxt:setFillColor(0,0,0) local function reload1( self,event ) if money\>10 then bullets1= bullets1+10 reloadtxt:removeSelf() end end reloadtxt:addEventListener("touch",reload1) end local function collision1( event ) bullet1:removeSelf() zombie1:removeSelf() money=money+math.random(0,2) print(bullets1) end bullet1:addEventListener("collision",collision1) end end end end timer.performWithDelay( 700, spawnSmiles, 0 ) --fire every 10 seconds

Two tips:

  1. Use ‘display.remove(obj)’ instead.  It is safer than ‘obj:removeSelf()’

  2. Remove the object after one frame:

    – Note: any time less than one frame period is the same as waiting one frame timer.performWithDelay( 1, function() display.remove(obj) end )

  3. Don’t use function listeners (use table listeners) + refine your collision listener to take actions on specific phases + remember to return a value.

    function bullet1.collision( self, event ) if( event.phase == “began” ) then timer.performWithDelay( 1, function() display.remove(bullet1) display.remove(zombie1) end ) money=money+math.random(0,2) end return true end bullet1:addEventListener(“collision”)

Two tips:

  1. Use ‘display.remove(obj)’ instead.  It is safer than ‘obj:removeSelf()’

  2. Remove the object after one frame:

    – Note: any time less than one frame period is the same as waiting one frame timer.performWithDelay( 1, function() display.remove(obj) end )

  3. Don’t use function listeners (use table listeners) + refine your collision listener to take actions on specific phases + remember to return a value.

    function bullet1.collision( self, event ) if( event.phase == “began” ) then timer.performWithDelay( 1, function() display.remove(bullet1) display.remove(zombie1) end ) money=money+math.random(0,2) end return true end bullet1:addEventListener(“collision”)