-- 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?
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.
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
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”?
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]
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]
-- 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”?