Level: Beginner
Using the Accelerometer
Contents
-
Introduction
-
How to Enable the Accelerometer (Stencyl 2.2 and below)
-
Accelerometer Values
-
How to Use the Values
Introduction
Some games use an accelerometer to create games a user can control by tilting the device. One famous example is the Labyrinth game.

How to Enable the Accelerometer (Stencyl 2.2 and below)
Note: In Stencyl 3.0 and above, this is no longer necessary. The accelerometer is always enabled.
To enable the accelerometer, click the Settings button...

Then click Mobile> User Input, as shown:

Set the Accelerometer value to 30 (shown at #3).
Note: The reason we disable the accelerometer by default is because it drains valuable processing time from touch input and can cause that touch input to lag. In Stencyl 3.0, the new engine does not suffer from this behavior, hence why it's always on.
30 FPS suffices for most games. A higher value makes accelerometer control smoother at the expense of a game’s framerate.
Accelerometer Values
You can access accelerometer data through a trio of blocks under the User Input > Mobile category in the Design Mode Palette.

The values of each of these range between -1.0 and 1.0 inclusive. Those values represent how much the device is tilted in a given direction.
For example, an x value of 1 would mean the user is tilting it all the way to the right. An x value of -1 would mean it’s tilted all the way to the left.

Note: The values are flipped for a landscape oriented game. In other words, the values are always relative to portrait orientation and don't automatically flip for a landscape game. See the example below for details.
Tip: Do not enable auto-rotation for a game that uses the accelerometer. This will cause the values to flip and generally create a confusing situation.
How to Use the Values
To replicate a basic tilting motion on a landscape-oriented game, create a simple behavior as shown in the image below. You can adjust the value “-70” to another that suits your needs.

Note: The reason why x and y are flipped is because this is for a landscape-oriented game. For a portrait-oriented game, flip the y and x blocks around.
In the Labyrinth demo game’s case, the negative value worked best to produce the type of gameplay we were looking for. Again, try out different values until you find one that works for your game.
Tip: If you find the values to be too sensitive/fidgety, add some code to throw out really low absolute values and require the device to be tilted further than some minimum threshold before responding.
Last Updated: 2013-04-10 by Jon
6622 have read this article
Disclaimer: The Stencyl Team does not actively monitor comments on articles. If you're seeking help for your game,
please ask a question on the forums. Thanks!
Sign In to Comment