removeSelf function being called after object removed issue

Hi,
I have a bee sprite which is spawned onto screen, it stays around for 7 seconds, disappears then another is spawned 5 seconds later. A total of twelve are spawned.

The bee has a collision filter and can be destroyed by “the player” during the 7 second it is on screen.
When it is destroyed another sprite sheet plays a single frame.

Now my issue is that if the bee is collided with during the 7 seconds I get an error after 7 seconds saying it has already been removed, obviously by the collision function.

If you could cast an eye over my code it would be good:
The error is on line 17 removeSelf, I can see why its happening but unsure how to fix it.
I have tried quite a few things this morning but seem to be getting nowhere.

----------bee.lua----------  
  
local function beeSpawn()  
 local sheet = graphics.newImageSheet( "images/bee.png", { width=57, height=55, numFrames=8 } )  
 local bee = display.newSprite( sheet, { name="bee", start=1, count=7, time=150 } )  
 bee:play()  
  
 bee.x = math.random(50,430)  
 bee.y = math.random(50,230)--first number=top screen, second=bottom,  
  
 physics.addBody(bee, "dynamic",{radius=25, filter=beeCollisionFilter})  
 bee.myName = "bee"  
 bee:addEventListener("collision", bee)  
 transition.from(bee, {time = math.random(1400,2500), delay = 0, y = bee.y +300,})  
  
 local function removeBee( event )  
 bee:removeSelf()  
 end  
  
 timer.performWithDelay( 7000, removeBee)  
  
 --bee collision play last frame--  
  
 function bee:collision (event)  
 local bee = event.target  
 bee.isVisible = false  
 local attack = newAttackSprite()  
 attack.x = bee.x  
 attack.y = bee.y  
 attack:play()  
 event.target:removeSelf()  
 if lives \> 0 then  
 lifeBar[lives].isVisible = false  
 lives = lives -1  
 lifeBar[lives].isVisible = true  
  
 --if lives == 0 then  
 --director:changeScene('scene\_gameover')  
 end  
 end  
 end  
  
 timer.performWithDelay(5000, beeSpawn, 12)  
  
 require "sprite"  
  
 function newAttackSprite()  
 local deadBee = sprite.newSprite(beeSet)  
 deadBee:prepare("default1")  
 deadBee.isHitTestable = false  
 return deadBee  
  
 end  
end  
--bee collision end --  
  
  
-----Main.lua------  
  
local function init()  
  
 --bee sheet dead--  
  
 beeAttackSheet = sprite.newSpriteSheet("images/bee.png" ,57 ,55)  
 beeSet = sprite.newSpriteSet(beeAttackSheet,1, 8 )   
 sprite.add(beeSet, "default1", 8, 8, 2000, 1 )  
  
end  
  
init()  

[import]uid: 127675 topic_id: 31003 reply_id: 331003[/import]

just cancel remove bee timer if the be has already been removed in the collision event.

[lua]local removeBeeTimer = timer.performWithDelay( 7000, removeBee)

-bee collision play last frame–

function bee:collision (event)
if removeBeeTimer then timer.cancel ( removeBeeTimer ); removeBeeTimer = nil end [import]uid: 13632 topic_id: 31003 reply_id: 123937[/import]

@ojnab

thank you for your help, I feel real stupid for not thinking to cancel the timer; I was trying to perform a function on the bee.
When I saw your post it immediately became apparent as to what to do.

Cheers you made me a happy man! [import]uid: 127675 topic_id: 31003 reply_id: 123942[/import]

just cancel remove bee timer if the be has already been removed in the collision event.

[lua]local removeBeeTimer = timer.performWithDelay( 7000, removeBee)

-bee collision play last frame–

function bee:collision (event)
if removeBeeTimer then timer.cancel ( removeBeeTimer ); removeBeeTimer = nil end [import]uid: 13632 topic_id: 31003 reply_id: 123937[/import]

@ojnab

thank you for your help, I feel real stupid for not thinking to cancel the timer; I was trying to perform a function on the bee.
When I saw your post it immediately became apparent as to what to do.

Cheers you made me a happy man! [import]uid: 127675 topic_id: 31003 reply_id: 123942[/import]

Another options is:

local function removeBee( event )
if (bee and bee.parent) then
display.remove(bee)
end
end [import]uid: 63787 topic_id: 31003 reply_id: 128583[/import]

Another options is:

local function removeBee( event )
if (bee and bee.parent) then
display.remove(bee)
end
end [import]uid: 63787 topic_id: 31003 reply_id: 128583[/import]