make sound when a object hit the floor

Hi everyone, I’m new here and I’m creating some routines, I would like to understand how can I make a sound when a object hits the floor. I have seen some examples and I came out with this code, but, doesn’t work.
I hope somenone can give and advice.

Thanks.

This should make a noice when the red square hit the floor

–*****Floor********************
local ground = display.newRect( 0, baseline, 480, 5 )
ground.x = _W / 2
ground.y = baseline
ground:setFillColor(0, 255, 0)
physics.addBody(ground, “kinematic”, {density = 1.0, friction = 1, bounce = 0.2})
ground.myName = “ground”
–***********************************

–****Red Square************
local object1 = display.newRect(0, 0, 50, 50)
object1.x = _W / 2
object1.y = _H / _H - 50
object1:setFillColor(255, 0, 0)
physics.addBody(object1, {density = 1.0, friction = 1, bounce = 0.2})
object1.myName = “object1”
–*****************************************

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )
if(event.object1 == object1 and event.object2 == ground) then
audio.play( squishSound )
end
elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end

object1.collision = onLocalCollision
object1:addEventListener( “collision”, object1 )

ground.collision = onLocalCollision
ground:addEventListener( “collision”, ground ) [import]uid: 54055 topic_id: 14076 reply_id: 314076[/import]

Not seeing your full source, I played around a bit with this and got the sound to play by modifying the following chunk…

local function onLocalCollision( self, event )  
if ( event.phase == "began" ) then  
  
 print( self.myName .. ": collision began with " .. event.other.myName )  
  
 --if(event.object1 == object1 and event.ground == ground) then  
 audio.play( squishSound )  
 --end  
elseif ( event.phase == "ended" ) then  
  
print( self.myName .. ": collision ended with " .. event.other.myName )  
  
end  
end  

Notice I just commented out a couple lines, if these are the only two objects raising the onLocalCollision events then you are all set that it is the ground and box colliding.

Also, I’m assuming you have somewhere else in your code this line to prime the audio pump so to speak…

local squishSound = audio.loadSound("squishSound.wav")  

Hopefully that gives you a hint on what to do next, but in doing that I got the sound to play, but it plays on every bounce until the box comes to rest. Adjust for your use.

Good luck!

Croisened
Here’s the full code sample I tweaked from your post to get it to test out…(just need a sound file and this runs)

local physics = require("physics")  
physics.start()  
  
\_W = display.contentWidth  
\_H = display.contentHeight  
local squishSound = audio.loadSound("squishSound.wav")  
  
---[[  
--\*\*\*\*\*Floor\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
local ground = display.newRect( 0, 400, 480, 5 )  
ground.x = \_W / 2  
ground.y = 400  
ground:setFillColor(0, 255, 0)  
physics.addBody(ground, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
ground.myName = "ground"  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
--]]  
--\*\*\*\*Red Square\*\*\*\*\*\*\*\*\*\*\*\*  
local object1 = display.newRect(0, 0, 50, 50)  
object1.x = \_W / 2  
object1.y = \_H / \_H - 50  
object1:setFillColor(255, 0, 0)  
physics.addBody(object1, {density = 1.0, friction = 1, bounce = 0.2})  
object1.myName = "object1"  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
---[[  
  
local function onLocalCollision( self, event )  
if ( event.phase == "began" ) then  
  
 print( self.myName .. ": collision began with " .. event.other.myName )  
  
 --if(event.object1 == object1 and event.ground == ground) then  
 audio.play( squishSound )  
 --end  
elseif ( event.phase == "ended" ) then  
  
print( self.myName .. ": collision ended with " .. event.other.myName )  
  
end  
end  
  
object1.collision = onLocalCollision  
object1:addEventListener( "collision", object1 )  
  
ground.collision = onLocalCollision  
ground:addEventListener( "collision", ground )  
  
--]]  
  

[import]uid: 48203 topic_id: 14076 reply_id: 51848[/import]

Amazing work!!, that works perfect.
Thanks very much.

Could you help me in something else?.
I was trying to improve my code and I inserted two new things:
1.- Stop the noise after the object hits the floor
2.- The object is constituted by two pictures, I’m trying to change the image frame one to the frame two. (how should I have to do if I whant a sprite (lots of frames) instead a single image?)

The problem is that the noise is still ringing after the contact with the floor, and I can’t make appear the second frame of the image. (the idea is change an egg to the broken egg)

This is part of the code which is important for this:

local movieclip = require( “movieclip” )
local ui = require( “ui” )

–*****SOUND BREAKING EGG***********
local squishSound = audio.loadSound(“squish_wav.wav”)
–************************************

physics = require(“physics”)
physics.start()
physics.setGravity(0,9.8)
local baseline = 290
_H = display.contentHeight
_W = display.contentWidth
mRand = math.random

–****Base Line********************
local ground = display.newRect( 0, baseline, 480, 5 )
ground.x = _W / 2
ground.y = baseline
ground:setFillColor(0, 255, 0)
physics.addBody(ground, “kinematic”, {density = 1.0, friction = 1, bounce = 0.2})
ground.myName = “ground”
–***********************************

–****Egg***********
eggBody = { density=1.0, friction=0.1, bounce=0.2, radius=25 }
object1 = movieclip.newAnim{ “egg.png”, “egg_cracked.png” }
object1.x = _W / 2
object1.y = _H / _H - 50
object1.id = “object1”
physics.addBody( object1, eggBody )
object1.myName = “object1”
–*****************************************

–***********Hit against the floor-----------
function startListening()
if ground.postCollision then
return
end
local function onLocalCollision2( self, event )
if ( event.phase == “began” ) then
print( self.myName … ": collision began with " … event.other.myName )
audio.play( squishSound )
self:stopAtFrame(2)
self:removeEventListener( “postCollision”, self )
elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end
ground.collision = onLocalCollision2
ground:addEventListener( “collision”, ground )
–*************************************************
end
timer.performWithDelay( 0, startListening ) [import]uid: 54055 topic_id: 14076 reply_id: 51873[/import]