2014-05-05 52 views
0

我创建了一个手榴弹和地面触及爆炸的场景,共有5个手榴弹可供玩家使用。问题是当抛出多于一个手榴弹时removeself函数被调用为最新手榴弹只有和前一个不是立即吹掉和删除。在一段时间后移除图像

if event.object1.myname=="ground" and event.object2.myname=="grenade2" then 
local ex2=audio.play(bomb,{loops=0}) 
health1=health1-1 
check() 
health1_animation:setFrame(health1) 
explosion_animation2=display.newSprite(explosion_sheet,explosion_sequence) 
explosion_animation2.x=event.object2.x 
explosion_animation2.y=event.object2.y 
explosion_animation2:play() 
end 
timer.performWithDelay(300,function() explosion_animation2:removeSelf() 
end,1) 

回答

1

您正在将explosion_animation2声明为全局变量,因此每次调用此冲突代码时都会覆盖它。你会希望有explosion_animation2为局部变量,以便在延迟函数中使用它会创建它周围的终止:如果由于某种原因,你依靠explosion_animation2是全球

local explosion_animation2 
if event.object1.myname=="ground" and event.object2.myname=="grenade2" then 
    local ex2=audio.play(bomb,{loops=0}) 
    health1=health1-1 
    check() 
    health1_animation:setFrame(health1) 
    explosion_animation2=display.newSprite(explosion_sheet,explosion_sequence) 
    explosion_animation2.x=event.object2.x 
    explosion_animation2.y=event.object2.y 
    explosion_animation2:play() 
end 
timer.performWithDelay(300,function() explosion_animation2:removeSelf() 
end,1) 

,你可以做一个本地副本相反:

if event.object1.myname=="ground" and event.object2.myname=="grenade2" then 
    local ex2=audio.play(bomb,{loops=0}) 
    health1=health1-1 
    check() 
    health1_animation:setFrame(health1) 
    explosion_animation2=display.newSprite(explosion_sheet,explosion_sequence) 
    explosion_animation2.x=event.object2.x 
    explosion_animation2.y=event.object2.y 
    explosion_animation2:play() 
end 
local closure_var=explosion_animation2 
timer.performWithDelay(300,function() closure_var:removeSelf() 
end,1)