Nov 192010
 

This tutorial will explain how to control an “actor” with the accelerometer in Corona.

*note: I call it an actor because I’m a GS convert, you’ll likely know it by another name but for now I’m stuck in my ways, sorry!

Download my tutorial project here.

*I borrowed the ball used in this project from a Corona template, Shape Tumbler.

This is not necessarily the only or best way to control your “actor” with the accelerometer, but it’s how I do it and thus the way I’ll be explaining to you.

If you want to see how it works, build the tutorial project for your device, as we cannot test the accelerometer on the simulator.

Now, for those who don’t want to download the sample and simply wish to add the code to their own project immediately, let’s get down to how to do that!

What you will see below is my entire main.lua file, see how short it is? That’s how simple it is to do this with Corona.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
display.setStatusBar (display.HiddenStatusBar)
-- Hides the status bar

local physics = require ("physics")
physics.start()
physics.setGravity(0,0)
-- start physics engine and set the gravity (I'm using 0 to start, you might want to change this.)

background = display.newImage ("background.png")
-- Sets the background

ball = display.newImage ("ball.png")
ball.x = 160
ball.y = 200
physics.addBody(ball, {friction = 1.0, bounce=0.6})
-- Adds the ball and adds physics to the ball

local motionx = 0
local motiony = 0

local function onAccelerate( event )
motionx = 35 * event.xGravity
motiony = 35 * event.yGravity
end
Runtime:addEventListener ("accelerometer", onAccelerate)

local function moveball (event)
ball.x = ball.x + motionx
ball.y = ball.y - motiony
end
Runtime:addEventListener("enterFrame", moveball)

-- Control motion with tilt

If you want to add this functionality to your own project, you will only need a little part of this code, as follows;

Step One: Add the following code to the start of your project if you are not already using physics;

1
2
3
local physics = require ("physics")
physics.start()
physics.setGravity(0,0)

*You may want to set the gravity differently to how I’ve done mine, which is 0,0 – this depends entirely on what you are aiming to achieve with your project.

Step Two: Add this line below the actor you want to control with the accelerometer, which adds physics to it;

1
physics.addBody(ball, {friction = 1.0, bounce=0.6})

You will of course change “ball” to whatever your actor is called.

Step Three: Add these two lines somewhere below the code you’ve just added to your project file;

1
2
local motionx = 0
local motiony = 0

This is important because it tells your app that motionx and motiony exist and that at the present time they are both = 0.

Step Four: Obviously, we don’t want motionx and motiony to stay as 0, we want them to change depending on tilt. Place the following code beneath what you’ve already added to your file;

1
2
3
4
5
local function onAccelerate( event )
motionx = 35 * event.xGravity
motiony = 35 * event.yGravity
end
Runtime:addEventListener ("accelerometer", onAccelerate)

What this does is update motionx and motiony accordingly; you may want to have a play around with numbers, but I find 35 works well for me.

Step Five: It’s finally time to make your “actor” respond to the accelerometer. Place the following code after what we just added;

1
2
3
4
5
local function moveball (event)
ball.x = ball.x + motionx
ball.y = ball.y - motiony
end
Runtime:addEventListener("enterFrame", moveball)

Of course you will rename your function something that suits your project, such as “movehero” or the like, and will replace “ball.x” and “ball.y” with “hero.x” and “hero.y”. This sounds simple, and it is, but if you forget to change any of these your project will not work as planned.

And that’s it!

Build your project for your device and you should have your actor moving around based on tilt. Of course, you will likely want to add walls or wrap your scene/screen so that the “actor” doesn’t disappear off the sides, but I’ll talk about that some other time.

As usual, if you have questions, ask! You can tweet me or find me on Facebook if I’m awake and I’m always happy to help if I can.

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!

 

  60 Responses to “Control An Actor With The Accelerometer”

  1. The download link gave me a pretty sad Error 404 – Not Found.

    :(

    • Hey Andi – you’re in luck, this is one of the few files that was left on my computer after I stopped using Corona. Try the link now, it will work.

  2. Can I have the tutorials too? I don’t have twitter.

    • You just commented on a huge number of my tutorials asking for links – the majority, including this one, do not require Twitter to download. Just click the download link.

  3. Clear tutorial! :)

    Might be an idea to mention system.setAccelerometerInterval();
    Can really make a difference..

 Leave a Reply

(required)

(required)

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.