Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Multiple sounds on collision listener, help!
Started by dodi_games Sep 02 2018 04:25 PM

2 replies to this topic

Best Answer Rob Miracle , 03 September 2018 - 10:39 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 267 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,527 posts
  • Corona Staff

  Best Answer

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



[TOPIC: post.html]
#3

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 267 posts
  • Corona SDK

Thanks @rob now I understand the concept.


[topic_controls]
[/topic_controls]