简体   繁体   English

电晕sdk无法在单个生成的对象上停止计时器

[英]Corona sdk having trouble stopping timers on individual spawned objects

I have a beat em up game and a 'punch' sound is played on a timer if an enemy collides with my character as shown below: 我有一个节拍游戏,如果敌人与我的角色发生碰撞,计时器上会播放“打孔”声音,如下所示:

local function hitSound()
local hsound = math.random(#sfx.hit)
audio.play(sfx.hit[hsound])
end

--------------------------------------------------CHARACTER COLLISION--------------- --------------------------------------------------角色冲突---------------

local function guyCollision(self, event)
if event.other.type == "enemy1"then

if event.phase == "began" then
hitTimer = timer.performWithDelay(500,hitSound,0)
event.other:setSequence("punch")
event.other:play()
end

if event.phase == "ended" then
timer.pause(hitTimer)
end

This works fine when my character is taking down just one enemy at a time but if there are more than one spawned enemy (which there usually is) fighting my character when i kill them the punching sound remains. 当我的角色一次只击落一个敌人,但是当我杀死他们时,如果有多个生成的敌人(通常有)与我的角色战斗时,这种方法效果很好。 Also when i call audio.fadeout() when my character dies the punch sounds don't fade out with the other sounds/music :s 另外,当我的角色死亡时,当我调用audio.fadeout()时,打孔声音不会随着其他声音/音乐而消失:

Someone suggested to me to assign the timer to the specific enemy in question using its index in its table but im unsure of how to do this... is this the best way? 有人建议我使用其表中的索引将计时器分配给有问题的特定敌人,但不确定如何执行此操作……这是最好的方法吗? I just don't know how to get that enemys current index.. any help much appreciated! 我只是不知道如何获取敌人的当前索引..任何帮助,不胜感激!

Heres my enemy spawn code: 这是我的敌人生成代码:

local spawnTable2 = {}


local function spawnEnemy()
enemy1 = display.newSprite( group, sheetE, sequenceData2 )
enemy1.x=math.random(100,1300)
enemy1.y=math.random(360,760)
enemy1.gravityScale = 0
enemy1:play()
enemy1.type="coin"
enemy1.objTable = spawnTable2
enemy1.index = #enemy1.objTable + 1
enemy1.myName = "enemy" .. enemy1.index
physics.addBody( enemy1, "kinematic",{ density = 1.0, friction = 0.3, bounce = 0.2 })
enemy1.isFixedRotation = true
enemy1.type = "enemy1"
enemy1.enterFrame = moveEnemy
Runtime:addEventListener("enterFrame",enemy1)
enemy1.objTable[enemy1.index] = enemy1
return enemy1
end

I think your problem is that your hitTimer variable is probably being overwritten and you can only cancel the last one. 我认为您的问题是您的hitTimer变量可能已被覆盖,您只能取消最后一个变量。 You could probably do: self.hitTimer to get around it. 您可能可以做:self.hitTimer可以解决它。

Rob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM