you can assign collision listener to the ball.
the collision code must come before the ball or the function won’t work.
example:
[lua]
local ballObject
local ground
– your audio here
local crashSound = audio.loadSound( “crash.aac” )
ground = display.newImageRect(“ground.png”, 480, 100)
ground.x = 0
ground.y = 0
ground.objectName = “ground”
physics.addBody(ground, “static”, {density = 10})
local onGroundCollision = function (self, event)
if event.phase == “began” then
if event.other.objectName == “ground” then
audio.play(crashSound)
– add whatever you want to happen when the collision
– happen along with the crash sound
end
end
end
ballObject = display.newImageRect( “ball.png” , 60 , 60 )
ballObject.x = 50
ballObject.y = 430
ballObject.objectName = “ball”
physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 30 } )
ballObject.collision = onGroundCollision
ballObject:addEventListener(“collision”, ballObject)
[import]uid: 12455 topic_id: 6643 reply_id: 23179[/import]