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]