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]