Corona SDK: Develop an Entertaining Magic Ball Application

Corona SDK: Develop an Entertaining Magic Ball Application

Tutorial Details
  • Technology: Corona SDK (Android, iOS)
  • Difficulty: Beginner
  • Completion Time: 30 Minutes

In this tutorial, we’ll use the Corona API’s and the Lua programming language to create an entertaining Magic Ball App.


Brief Overview

Corona SDK Magic Ball - Figure 1

Using the Shake Event built in the Corona API, we’ll create an application that generates a random result from predefined words.You’ll also learn to create simple animations using the transition methods.


Select Target Device

Corona SDK Magic Ball - Figure 2

The first thing you have to do is select the platform you want to run your app, this way you’ll be able to choose the size for the images you will use.

The iOS platform has these characteristics:

  • iPad: 1024x768px, 132 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone 4: 960x640px, 326 ppi

For Android is a little different, being an open platform, you may encounter many different screen resolutions:

  • Nexus One: 480x800px, 254 ppi
  • Droid: 854x480px, 265 ppi
  • HTC Legend: 320x480px, 180 ppi

In this tutorial we’ll be using the iPhone/iPod touch as the platform.


Interface

Corona SDK Magic Ball - Figure 3

This is the graphic interface we’ll be using, it includes a triangle graphic that will serve as the Octohedron found in Magic Balls.


Exporting PNG’s

Corona SDK Magic Ball - Figure 4

Depending on the device you have selected you will need to export the graphics in the recommended PPI, you can do that in your favorite image editor.

I used the Adjust Size… function in the Preview app in Mac OS X.

Remember to give the images a descriptive name and to save them in your project folder.


Code!

Corona SDK Magic Ball - Figure 5

Time to write our application!

Open your prefered Lua editor (any Text Editor will work, but you won’t have syntax highlighting) and prepare to write your awesome app.


Hide Status Bar

First, we hide the status bar, this is the bar on top of the screen that shows the time, signal and other indicators.

display.setStatusBar(display.HiddenStatusBar)

Background

Now we add the app background.

local background = display.newImage("background.png")

This line creates the local variable background and uses the display API to add the specified image to the stage. By default, the image is added to 0,0 using the top left corner as the reference point.


Octohedron

We repeat the process with the octohedron image, placing it in the center of the stage.

local octohedron = display.newImage("octohedron.png", 110, 186)
octohedron.isVisible = false

The Octothedron will be invisible by default, and will appear at the first device shake.


TextField

The following code creates the center TextField that will display the random sentence when a shake event is dispatched.

local textfield = display.newText("", 0, 0, native.systemFontBold, 14) -- Create the TextField
textfield:setReferencePoint(display.CenterReferencePoint) -- Change reference point to center for easy positioning
textfield.x = display.contentWidth * 0.5 -- Center TextField
textfield.y = display.contentHeight * 0.5
textfield:setTextColor(255, 255, 255) -- Set text color to white

Necesary Variables

The next variables will be used to handle the Shake event.

  • shake: A Table that will be used as a function listener for the shake event.
  • options: Stores the words that can be shown by the magic ball.
local shake = {}
local options = {"Probably Not", "No.", "Nope", "Maybe", "Yes", "Probably", "It's Done", "Of Course"}

Shake Function

This function listens for a shake event and reveals the octohedron and text if true.

function shake:accelerometer(e)
if(e.isShake == true) then
octohedron.isVisible = true
transition.from(octohedron, {alpha = 0}) -- Show octohedron, animates alpha from 0 to 1
textfield.text = options[math.random(1, 8)] -- Selects a random sentence from the options variable
transition.from(textfield, {alpha = 0}) -- fades in the text
end
end

Accelerometer Listener

The Accelerometer events are runtime based, so we use the Runtime keyword to add the listener.

Runtime:addEventListener("accelerometer", shake)

Icon

Corona SDK Magic Ball - Figure 6

If everything is working as expected, we are almost ready to build our app for device testing. Just one more thing, our application icon.

Using the graphics you created before you can create a nice and good looking icon, the icon size for the iPhone icons is 57x57px, but the iTunes store uses a 512x512px so it’s better to create your icon in this size.

It doesn’t need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.


Conclusion

Shake events are easy to detect and really useful, you can give them any functionality such as erasing, undo, shuffle or get a random answer just like you did in this tutorial.

Thanks for reading!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Christian Wisniewski

    Nice Post!
    I’m curious if it wouldn’t be better to make it more dynamic, changing

    textfield.text = options[math.random(1, 8)]

    so it would actually get the length of the options-object instead of using hardcoded length..otherwise maintaining might become an issue for bigger projects.

    I think I might take a closer look into Corona :) Seems to be quiet good for quickly developing smaller apps!

    • http://www.zainals.com Moe

      I think this should do it

      local rand = options[math.random(1, #options)]

  • Dan

    Great Article!
    However, when I ran the program and tested in my LG Ally, I had to vigorously shake the Android to get it to change.

    Is there any way to change the sensitivity of the accelerometer?

  • http://ppeillard.com Pablo Peillard

    Well, Corona is really flexible. If you change the accelerometer system interval you could make in more responsive. Something that you could add at the start of the code is “system.setAccelerometerInterval(100)” (w/o quotes) the number can be from 10-100.

  • http://www.zainals.com Moe

    Thank you Carlos :-) thats really a good way to use the acc.

    I noticed that it doesn’t work correctly when you do multiple shakes so I added the following under the declarations:

    local isShaking = false

    function notShaking()

    isShaking = false

    end

    I then amended the if statement:
    if(e.isShake == true and isShaking == false ) then
    isShaking = true

    then added a callback function to the transition:

    transition.from( textField , {alpha = 0, onComplete=notShaking})

  • steve

    I’m wondering if this can be used for a simple daily sayings type app, I’ve been wanting to write one as an absolute novice. I guess I can put in different sayings and change the 8 ball look a little, but the engine should work.?

  • Kurt

    LOVE thanks