Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Timer 经过一段时间后移除图像_Timer_Lua_Listener_Collision Detection_Coronasdk - Fatal编程技术网

Timer 经过一段时间后移除图像

Timer 经过一段时间后移除图像,timer,lua,listener,collision-detection,coronasdk,Timer,Lua,Listener,Collision Detection,Coronasdk,我已经创建了一个场景,手榴弹和接地爆炸发生,玩家总共有5枚手榴弹。问题是当有多枚手榴弹被扔到远处时,只为最新的手榴弹调用self函数,而前一枚手榴弹不会被立即引爆和移除 if event.object1.myname=="ground" and event.object2.myname=="grenade2" then local ex2=audio.play(bomb,{loops=0}) health1=health1-1 check() health1_animation:set

我已经创建了一个场景,手榴弹和接地爆炸发生,玩家总共有5枚手榴弹。问题是当有多枚手榴弹被扔到远处时,只为最新的手榴弹调用self函数,而前一枚手榴弹不会被立即引爆和移除

 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)

您将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)
如果出于某种原因,您依赖于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
local closure_var=explosion_animation2
timer.performWithDelay(300,function() closure_var:removeSelf()
end,1)