collision sound problem

Hi all! I’m having problems with sounds in collisions, not basic sounds, but sounds that come out in a specific time. My game is similar to a pinball game and the problem I have is that I want to disappear an object in a certain time and play a sound when it disappears, not when the collision occurs.

This is the code that is inside the collision listener

--initially the boolean playSound is false. local function onLocalCollision( self, event )     if (self.id == "ball" and event.other.id == "obt\_1") or       (self.id == "ball" and event.other.id == "obt\_2") or       (self.id == "ball" and event.other.id == "obt\_3") or       (self.id == "ball" and event.other.id == "obt\_4") or       (self.id == "ball" and event.other.id == "obt\_5") then       print("ball and obt collision")       audio.play( pin ) --official audio of the collision playSound = true       remove = timer.performWithDelay(1000, function()         if playSound == true then           audio.play( clear )           playSound = false           print( "audio payed" )         end   display.remove( event.other )         print( event.other.id )         print( "obt removed" ) end ) end end 

Each time the ball collides with the other object playSound is true and the timer is supposed to run.

The problem is when the ball collides in less than 1 second with another object with a similar characteristic, the object disappears but no sound is heard. 

Thanks in advance

DoDI

Your variable playSound is not local, but global.

So when there are two collisions, the playSound variable turns true two times, then goes false after the deleting of the first, and still remain false after, so the if condition for the second object is not solved.

just write “local” before playSound

@yvandotet

Thanks for your quick response!

I put it local and now I have repeated sounds. If the ball touches the same object twice, before 1 second, the object disappears but the sound is heard twice

I’m thinking of playing the sound only if the object is removed, if there is no object removed there is no sound, but, I do not know how to verify that inside the collision listener because those objects are all the same and are created in a table.

Your variable playSound is not local, but global.

So when there are two collisions, the playSound variable turns true two times, then goes false after the deleting of the first, and still remain false after, so the if condition for the second object is not solved.

just write “local” before playSound

@yvandotet

Thanks for your quick response!

I put it local and now I have repeated sounds. If the ball touches the same object twice, before 1 second, the object disappears but the sound is heard twice

I’m thinking of playing the sound only if the object is removed, if there is no object removed there is no sound, but, I do not know how to verify that inside the collision listener because those objects are all the same and are created in a table.