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


Hey Andrea,
I don’t often deal with Android but a few things I do know;
1) You need to give the icons different names; Icon-ldpi.png, Icon-mdpi.png and Icon-hdpi.png (36×36, 48×48 and 72×72 pixels, respectively.)
2) Default.png does not currently do anything on Android as it is iOS specific.
3) The project name is coming from somewhere, if you didn’t enter it as the project name did you enter it in the identifier section? com.tryrobuild.whatever? Have you previously used that name?
Ok…
About the point 1), I have triplicated icon.png and renamed each of them as you are telling me (I see the warning about that in the Corona Simulator.exe window during the project build) but, in this way, all the files Icon-ldpi.png, Icon-mdpi.png and Icon-hdpi.png have the same size.
Is the difference between each icon-xdpi.png a “must” that have to be respected?
About the last point, probably I don’t know how to compile correctly the build window fields.
Trytobuild is the application name and I have put it in the “Application Name” field.
About the package, I don’t remember exactly the package typed in my trial, probably something like com.trytobuild.games. Is it wrong? How it should be compiled? Is there some rule that I have to respect for the package name creation?
However it was the first time I used that package name.
Thanks again for the help! ^^
Hey again,
Yes, the icons need those sizes I mentioned.
That name is showing as you put it as they Application name – com.yourname.trytobuild would be the correct identifier to use.
I’m not sure what the issue is exactly, I thought it was the name but now I feel like I’m missing something.
Sorry – can you elaborate further? Else, maybe post in the Android sub forum, Josh checks in there and he has more knowledge than I do about Android.
Hey Chris, apologies, this ended up in spam for some reason. Glad to see the issue is now resolved
Clear tutorial!
Might be an idea to mention system.setAccelerometerInterval();
Can really make a difference..
Definitely – an excellent point! I may have to add that in sometime