Hi,
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])</div>
end
--------------------------------------------------CHARACTER COLLISION--------------------------------------
<div>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.
I’ve tried putting the timer.pause in the function where the enemy gets removed which didnt work, tried timer.cancel instead of timer.pause aswell (grasping at straws)!
What i’m thinking is i maybe need an if statement around the audio.play to check if the sound is already playing, if it is then return, if it’s not then play? because the sounds are doubling up the more enemies im fighting then theyre not all getting paused/cancelled when i kill them (which i cant figure out why) just not sure how to check if that sound is playing? am i even on the right lines?
Also when i call audio.fadeout() when my character dies the punch sounds don’t fade out with the other sounds/music :s
Any help appreciated!!! thanks!!!