Free .WAV Wednesday — Click #6

Free .WAV Wednesday - Click #6 - ThumbnailI’ve been trying to learn Corona. The plan was to write a new textbook about Ansca Mobile’s software. But learning Lua has been a bit tricky for me. That’s why I’ve stopped the project. Before writing a book, one should master the topic. I’m not anywhere near as skilled with Corona as I am with GameSalad. However, I was at least able to produce some new educational material — a Corona Sound Effect Tutorial. That’s what what this article is about. The free .WAV Wednesday series returns, with a simple lesson on using sound effects in Corona.

Getting started with Corona is easy. There’s a free trial available at the official website (http://www.anscamobile.com/corona/) and there’s lots of documentation. You might want to start with something simple, like the traditional “Hello World” sample.

Once you get past that, you should realize something significant about game development in Corona. Most of the work is done in a main.lua file. I was shocked at how straightforward it was. I only needed a folder and a blank text document to start a new project. I found this to be a lot easier than Xcode. Although, it took me a while to realize “File > New Project” has no meaning in Corona. If you want to work with this SDK, you’re going to need a good text editor.

So OK, let’s get to it. Here’s the code…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--> Clicky Button
 
click6 = audio.loadSound("click-6.wav")
 
local button = display.newCircle(display.contentWidth / 2, display.contentHeight / 2, 50)
button:setFillColor(150,200,255)
 
function button:touch(event)
  if event.phase == "began" 
  then audio.play(click6)
  end
end
 
button:addEventListener("touch", button)

The first line is a comment. It’s just a little note to myself or anyone that reads the code. You don’t need that, but it’s a good programming habit. You can start a comment line in Lua with a double hyphen.

By placing an audio file in the project folder, it becomes one of your game assets. Yet, this isn’t loaded automatically. It has to be loaded with Lua code. That’s what the next line of code is about. The click-6.wav file is loaded into memory.

The next part of the sample code is creating a button. It’s a circle that is aligned to the middle of the screen and it has a 50 pixel radius. The “Fill Color” numbers are the Red, Green and Blue values. I called my circle “button”, but you can use a different name. I recently learned that hyphens make for bad names. A name like “clicky-button-6” can break the app.

With the circle created, the next step is actually making it sensitive to touches. Basically, the logic is in two parts — If there’s a touch event on the circle, and if that event just “began”, then play the audio file.

The final line of code is a listener. The application constantly checks the circle’s function. This was confusing to me, but that’s how an app is optimized. Instead of checking every graphic element in the game for activity, only the ones you specify are checked. I’m still getting used to that part of Corona. But even in just this short block of code, basic gaming elements are unleashed. It’s a button that makes a sound. Ah, it reminds me of the arcades.

You can try download the free sound file and test out the code…

If you want more sound effects, you can purchase a license to the complete Sound Effects — By Photics collection. The free sound effect file is provided under the same license as the full sound effects collection.