Sound on contact

I am new to corona.
I have created a ball:

-- Load Ball ballObject = display.newImageRect( "ball.png" , 60 , 60 ) ballObject.x = 50 ballObject.y = 430 physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 30 } )Every time the ball touches a static surface I want it to make a sound (is .caf best for sort sounds?). How can I do this?

Thanks! [import]uid: 15281 topic_id: 6643 reply_id: 306643[/import]

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]

Hey, Thanks for the reply, your code worked great!
I just had one question: if I get my ball like this:

-- Load Ball local ball = require("ball")
How can I apply the sound to it (seeing as it is made in a different file)? I do this because I use the ball in multiple places.

Thanks.

– [import]uid: 15281 topic_id: 6643 reply_id: 23261[/import]

Hmm… I’m not sure myself. If you know please post the solution. Thank you. [import]uid: 12455 topic_id: 6643 reply_id: 23409[/import]

Hey,

Does anyone know?
If I get my ball like this how I would apply a sound on contact?

-- Load Ball  
local ball = require("ball")  

Thanks! [import]uid: 15281 topic_id: 6643 reply_id: 23715[/import]

you need to post your ball.lua code (or the relevant parts that create it) and the relevant part of your main code that instantiates it, so we can see the setup/physics/listeners etc

j
[import]uid: 6645 topic_id: 6643 reply_id: 24139[/import]

Thanks for the reply!
This is the contens of my “ball.lua”:

-- Load Shadow  
local shadow = display.newCircle( 160 , 120 , 16 )  
shadow:setFillColor( 0 , 0 , 0 )  
shadow.alpha = 0.2  
shadow.yScale = 1  
  
-- Load Ball  
ballObject = display.newImageRect( "ball.png" , 30 , 30 )  
ballObject.x = 50  
ballObject.y = 300  
physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 15 } )  
  
-- Apply Force To Ball  
function applyForceToBall( xForce , yForce )  
  
 -- Apply Force To Ball  
 ballObject:applyForce( xForce , yForce , ballObject.x , ballObject.y )  
  
 -- Move Ball To Front  
 ballObject:toFront()  
  
end  
  
-- Follow Ball  
local function followBall()  
  
 -- Move Shadow To Ball  
 shadow.x = ballObject.x  
 shadow.y = ballObject.y + 5  
  
end  
Runtime:addEventListener( "enterFrame" , followBall )  

This is my “level_1.lua” (were I use the ball):

module(..., package.seeall)  
  
--------------------physics--------------------  
  
-- Setup Physics  
local physics = require ("physics")  
physics.start()  
physics.setGravity (0,0)  
  
--------------------ball--------------------  
  
-- Load Ball  
local ball = require("ball")  
  
-- Movement Function  
local function movement()  
  
 -- Get X & Y Accelerometer Input From Remote  
 local xAcceleration = remote.xGravity  
 local yAcceleration = remote.yGravity  
  
 -- Convert Input To Force  
 local xForce = xAcceleration \* 55  
 local yForce = yAcceleration \* 55  
  
 -- Apply Force To Ball  
 ball.applyForceToBall( xForce , -yForce )  
  
end  
Runtime:addEventListener( "enterFrame", movement )  

As you can see I use corona remote.
So, should I add the “sound code” to my “ball.lua” or “level_1.lua”?

Thanks!

– [import]uid: 15281 topic_id: 6643 reply_id: 24250[/import]

if ball.lua isn’t a module, does that actually work for creating 2 balls? i didn’t think it would.
[import]uid: 6645 topic_id: 6643 reply_id: 24252[/import]

Hey “ball.lua” is a module. Sorry! I just failed to copy the whole thing!

Update:

Does any on know how I could do this? [import]uid: 15281 topic_id: 6643 reply_id: 24255[/import]

Hey, does any one know?
Is this even possible?

Thanks. [import]uid: 15281 topic_id: 6643 reply_id: 24808[/import]

sure it’s possible. do you only ever have one ball? because the code you are using I dont think allows you to create more than one. I don’t know if that will be a problem for you? [import]uid: 6645 topic_id: 6643 reply_id: 24831[/import]

Yes, right now I only plan to have one ball.
Do you know how I can do this with one ball?

Thanks! [import]uid: 15281 topic_id: 6643 reply_id: 24848[/import]

Hey,

I found a way that could work:

-- your audio here  
local crashSound = audio.loadSound( "hit.caf" )  
  
--------------------top wall with sound--------------------  
  
-- Top Wall  
local WoodBoardTop = display.newImage( "wall\_top.png" )  
WoodBoardTop.y = 6.25  
WoodBoardTop.x = 160  
WoodBoardTop.objectName = "WoodBoardTop"  
physics.addBody(WoodBoardTop, "static", { bounce = 0.1, friction = 4} )  
   
local onWoodBoardTopCollision = function (self, event)  
   
 if event.phase == "began" then  
 if event.other.objectName == "WoodBoardTop" then  
  
 audio.play(crashSound)  
  
 end  
 end  
end  
  
--------------------Bottom wall with sound--------------------  
  
-- Bottom Wall  
local WoodBoardBottom = display.newImage( "wall\_bottom.png" )  
WoodBoardBottom.y = 473.5  
WoodBoardBottom.x = 160  
WoodBoardBottom.objectName = "WoodBoardBottom"  
physics.addBody(WoodBoardBottom, "static", { bounce = 0.1, friction = 4} )  
   
local onWoodBoardBottomCollision = function (self, event)  
   
 if event.phase == "began" then  
 if event.other.objectName == "WoodBoardBottom" then  
  
 audio.play(crashSound)  
  
 end  
 end  
end  
  
--------------------ball--------------------  
  
-- Load Ball  
ballObject = display.newImageRect( "ball.png" , 30 , 30 )  
ballObject.x = 50  
ballObject.y = 430  
physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 15 } )  
ballObject.collision = onWoodBoardBottomCollision  
ballObject:addEventListener("collision", ballObject)  
ballObject.collision = onWoodBoardTopCollision  
ballObject:addEventListener("collision", ballObject)  

Now, as you can see there are some problems (it does not work for both walls!).
I know this code it just a huge mess (sorry I am new to all this)… how can I have 2 walls with sound apply to 1 “ball”?

Thanks.

– [import]uid: 15281 topic_id: 6643 reply_id: 25331[/import]

Anyone know? [import]uid: 15281 topic_id: 6643 reply_id: 26216[/import]