2014-05-05 56 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)