Multiple sounds on collision listener, help!

Hi guys I’m experimenting some issues trying to play 3 sounds on local collision listener.

Here is the code:

local function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( "ball bouncing everywhere" ) audio.play(bounce) --this play the sound on every collision\*\*\* if (self.id == "ball" and event.other.id == "win") then print("ball and win rect collision") if goNext == false then goNext = true event.contact.bounce = 0 ball.fill = loads.winBall --\*\*\*I WANT TO PLAYA DIFFERENT AUDIO HERE BUT I CAN'T STOP THE BOUNCE AUDIO\*\*\* tmr1 = timer.performWithDelay(1, jointObjects) tmr2 = timer.performWithDelay(1000, levelCompleted) tmr3 = timer.performWithDelay(3000, nextLevel) end elseif self.id == "ball" and event.other.id == "lost" then print("ball and up wall collision") if goReload == false then goReload = true event.contact.bounce = 0 ball.fill = loads.lostBall --\*\*\*I WANT TO PLAY A DIFFERENT AUDIO HERE TOO\*\*\* tmr4 = timer.performWithDelay(1000, reloadLevel) end end end end

The thing is I have 4 wall on screen borders but the one on top is the lost id in the collision listener, so I want to play a “crash ball” sound when the ball hit that wall, and I want to play a “win sound” when the ball hit the rectangle with the id “win”

I try:

if (self.id == "ball" and event.other.id == "leftWall") or (self.id == "ball" and event.other.id == "rightWall") or (self.id == "ball" and event.other.id == "bottomWall") then audio.play( bounce ) end

but if I make the “walls” comparision no function trigger when “ball” hit “win”

Thank in advance

DoDi

It sounds to me like you don’t really want to play the bounce sound for every collision. Instead maybe you should consider:

local function onLocalCollision( self, event ) if ( event.phase == "began" ) then if (self.id == "ball" and event.other.id == "win") then print("ball and win rect collision") if goNext == false then goNext = true event.contact.bounce = 0 ball.fill = loads.winBall --\*\*\*I WANT TO PLAYA DIFFERENT AUDIO HERE BUT I CAN'T STOP THE BOUNCE AUDIO\*\*\* tmr1 = timer.performWithDelay(1, jointObjects) tmr2 = timer.performWithDelay(1000, levelCompleted) tmr3 = timer.performWithDelay(3000, nextLevel) end elseif self.id == "ball" and event.other.id == "lost" then print("ball and up wall collision") if goReload == false then goReload = true event.contact.bounce = 0 ball.fill = loads.lostBall --\*\*\*I WANT TO PLAY A DIFFERENT AUDIO HERE TOO\*\*\* tmr4 = timer.performWithDelay(1000, reloadLevel) end else audio.play(bounce) --this play the sound only if no special collision happens end end end

If you want the bounce sound to always play and then play another sound when it’s complete, you can look at audio.play()'s “onComplete” feature to play your next sound. In your other collision conditions, you can set up a variable, perhaps named “nextSound”  and set it to the sound object you want to play next. Then your onComplete function could play nextSound if it’s not nil.
 

local nextSound = nil local function playNextSound( event )     if nextSound then         audio.play( nextSound )     end end

local function onLocalCollision( self, event ) if ( event.phase == "began" ) then nextSound = nil if (self.id == "ball" and event.other.id == "win") then print("ball and win rect collision") if goNext == false then goNext = true event.contact.bounce = 0 ball.fill = loads.winBall nextSound = winSound --\*\*\*I WANT TO PLAYA DIFFERENT AUDIO HERE BUT I CAN'T STOP THE BOUNCE AUDIO\*\*\* tmr1 = timer.performWithDelay(1, jointObjects) tmr2 = timer.performWithDelay(1000, levelCompleted) tmr3 = timer.performWithDelay(3000, nextLevel) end elseif self.id == "ball" and event.other.id == "lost" then print("ball and up wall collision") if goReload == false then goReload = true event.contact.bounce = 0 ball.fill = loads.lostBall nextSound = loseSound --\*\*\*I WANT TO PLAY A DIFFERENT AUDIO HERE TOO\*\*\* tmr4 = timer.performWithDelay(1000, reloadLevel) end end print( "ball bouncing everywhere" ) audio.play(bounce, {onComplete = playNextSound) --this play the sound on every collision\*\*\* end end

Or something similar (code not tested and a lot of assumptions being made)

Rob

Thanks @rob now I understand the concept.

It sounds to me like you don’t really want to play the bounce sound for every collision. Instead maybe you should consider:

local function onLocalCollision( self, event ) if ( event.phase == "began" ) then if (self.id == "ball" and event.other.id == "win") then print("ball and win rect collision") if goNext == false then goNext = true event.contact.bounce = 0 ball.fill = loads.winBall --\*\*\*I WANT TO PLAYA DIFFERENT AUDIO HERE BUT I CAN'T STOP THE BOUNCE AUDIO\*\*\* tmr1 = timer.performWithDelay(1, jointObjects) tmr2 = timer.performWithDelay(1000, levelCompleted) tmr3 = timer.performWithDelay(3000, nextLevel) end elseif self.id == "ball" and event.other.id == "lost" then print("ball and up wall collision") if goReload == false then goReload = true event.contact.bounce = 0 ball.fill = loads.lostBall --\*\*\*I WANT TO PLAY A DIFFERENT AUDIO HERE TOO\*\*\* tmr4 = timer.performWithDelay(1000, reloadLevel) end else audio.play(bounce) --this play the sound only if no special collision happens end end end

If you want the bounce sound to always play and then play another sound when it’s complete, you can look at audio.play()'s “onComplete” feature to play your next sound. In your other collision conditions, you can set up a variable, perhaps named “nextSound”  and set it to the sound object you want to play next. Then your onComplete function could play nextSound if it’s not nil.
 

local nextSound = nil local function playNextSound( event )     if nextSound then         audio.play( nextSound )     end end

local function onLocalCollision( self, event ) if ( event.phase == "began" ) then nextSound = nil if (self.id == "ball" and event.other.id == "win") then print("ball and win rect collision") if goNext == false then goNext = true event.contact.bounce = 0 ball.fill = loads.winBall nextSound = winSound --\*\*\*I WANT TO PLAYA DIFFERENT AUDIO HERE BUT I CAN'T STOP THE BOUNCE AUDIO\*\*\* tmr1 = timer.performWithDelay(1, jointObjects) tmr2 = timer.performWithDelay(1000, levelCompleted) tmr3 = timer.performWithDelay(3000, nextLevel) end elseif self.id == "ball" and event.other.id == "lost" then print("ball and up wall collision") if goReload == false then goReload = true event.contact.bounce = 0 ball.fill = loads.lostBall nextSound = loseSound --\*\*\*I WANT TO PLAY A DIFFERENT AUDIO HERE TOO\*\*\* tmr4 = timer.performWithDelay(1000, reloadLevel) end end print( "ball bouncing everywhere" ) audio.play(bounce, {onComplete = playNextSound) --this play the sound on every collision\*\*\* end end

Or something similar (code not tested and a lot of assumptions being made)

Rob

Thanks @rob now I understand the concept.