Expanding on the Shake Event

In last weeks Corona SDK tutorial, I explained how to use the shake event in Corona SDK. In this weeks post, I would like to expand on the functionality of the shake event and how to create a game similar to the magic 8 ball.

The magic 8 ball is a fortune telling toy that when shaken, gives a fortune. The fortune varies from very clear answers of ‘Yes’ or ‘No’ to answers that aren’t so clear such as ‘Cannot predict now’ or ‘Ask again later’. Let’s get started!

 

Our very first step should be to hide the status bar.

-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )

Next, let’s set up a background image that is white and put a title on the screen.

-- Create a background image that takes up the entire screen
background = display.newRect(0,0,display.contentWidth,display.contentHeight )
background:setFillColor( 255, 255, 255)

-- Create a game title
title = display.newText("Magic 8 Ball Game", 50, 0, native.systemFont, 60)
title:setTextColor(0, 0, 0)

Now that we have a background and a title set up, let’s put a magic 8 ball on the screen. I created a simple 8 ball using Photoshop, but I am no graphic artist. Feel free to download my Photoshop file and edit the image to make it look better or use your own images.

-- Put a magic 8 ball on the game
ball = display.newImageRect("Magic8Ball.png", 600, 593)
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2

This next step can be kind of tricky if you do not have experience with tables. A table allows you to store more information than a single variable. For example, if you have 4 answers to a question, you can store those 4 answers into a table instead of 4 different variables. Once you store these values in a variable, you can access these variables through the table index. For now, let’s just create a table and store some answers.

-- Create a table to hold all available answers
answers = {"Yes", "No", "Maybe", "Possibly", "Nope", "Sure"}

If you do not have experience with tables, I strongly suggest looking at the Corona Docs. The Corona Docs offers more information about tables and what you can do with them.

Moving on, we now need to create a text field that will store the answers. We will start the text field with the words ‘Shake Me’ to give some basic directions to the player.

-- Create a text object to display an answer
answerText = display.newText("Shake Me", 0, 0, native.systemFont, 24)
answerText.x = display.contentWidth/2
answerText.y = display.contentHeight/2 + 20

After we create the answer text field, we need to create a function that will change the text within the field. We will be using the same function in the previous tutorial – www.thatssopanda.com/using-the-shake-event-with-corona-sdk/.

-- Create a function that changes the background color and displays a random answer
function changeBackgroundAndAnswer()
    -- Set the background to a different color
    background:setFillColor(math.random(1,255),math.random(1,255),math.random(1,255))
    -- Get a random number based on length of answers table, in this case temp will be assigned the number 6
    temp = math.random(1,#answers)
    -- Change the answer on the magic 8 ball
    answerText.text = answers[temp]
end

In the function above, we created a function that changes the background to a randomly assigned color and we change the text in the answerText field. The answerText field receives a random answer from the answers array. Finally, we now set up the Shake event in Corona SDK.

-- Create a function to be called when the phone or mobile device is shaken
local function onShake (event)
    if event.isShake then
        -- Device was shaken, so call function
        changeBackgroundAndAnswer()
    end
end
-- Create a runtime listener for the shake event
Runtime:addEventListener("accelerometer", onShake)

The function above detects shake events and when the shake event is detected, the changeBackgroundAndAnswer() function is called. And that’s it! This is a fully working game using the Corona SDK shake event. Although this is a small game, the code I’ve provided can be a good base for you to start your next project.

Download Source Code

If you would like to see more tutorials, please leave a comment. Or, support me by purchasing one of my apps! Thanks for reading!


Leave a Reply