May 282012

How to detect and respond to a shake event in your CoronaSDK application.

This very simple (and as a result, short) example will show you how to detect a shake event in your app.

local function onShake (event)
if event.isShake then
print "Device was shaken"
Runtime:addEventListener("accelerometer", onShake)

This is of course more basic than many of my other tutorials but nonetheless I feel it is a useful addition to the Techority library ;)

As you can see we are using a Runtime listener tied to the accelerometer (which is of course how we detect any movement of the device, including a shake event) and then printing a statement.

To test this in the Corona simulator you can press CMD+up and to test on device make sure your device is hooked up to a computer so you can view the Xcode console. Alternatively rather than using a print statement you could make an object appear on the screen, show some text, etc.

This will work on both iOS and Android.

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!


 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.