Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

collision sound problem
Started by dodi_games Mar 11 2019 12:43 PM

3 replies to this topic
sound collision
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 348 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 184 posts
  • Corona SDK

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


  • dodi_games likes this

[TOPIC: post.html]
#3

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 348 posts
  • Corona SDK

@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



[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 348 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: sound, collision