What’s a game without sound? Sound and music can turn a boring humdrum game or movie into a riveting adventure, if done correctly!
In this tutorial we are going to show the process to add a sound effect to a Corona app.
When working with Corona, you are encouraged to use the CAF file type for your small audio files. CAF was developed by Apple for use with it’s newer operating system to replace the wav and AIFF file formats.
Place your sound effect file in the same folder as your main.lua and png graphic files.
I’m starting with the main.lua file previously created for fading in and out:
local textObj = display.newText(“Hello World!”, 50, 50, nil, 24)
textObj:setTextColor( 255, 255, 255)
local mybutton = display.newImage( “button.png” )
mybutton.x = display.contentWidth/2
mybutton.y = display.contentHeight-75
local h = textObj.height
local w = textObj.width
function mybutton:tap( event )
textObj.alpha =0
textObj.x = math.random(w/2, display.contentWidth – (w/2))
textObj.y = math.random(h/2, display.contentHeight -(100 + h/2))
transition.to (textObj, {time=3000, alpha=1})
end
mybutton:addEventListener( “tap”, mybutton )
We are going to add one line of code right after the function:
media.playEventSound(“beep.caf”)
So our final function looks like:
function mybutton:tap( event )
media.playEventSound(“beep.caf”)
textObj.alpha =0
textObj.x = math.random(w/2, display.contentWidth – (w/2))
textObj.y = math.random(h/2, display.contentHeight -(100 + h/2))
transition.to (textObj, {time=3000, alpha=1})
end
See the process demonstrated:
[tubepress video=M9p6XKk0kLo]
Note: I have changed display.stageWidth and display.stageWidth to display.contentWidth & display.contentHeight for version 2.0 of Corona. As of 2.0 stageHeight & stageWidth have been depreciated.