Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [42]

By Root 2551 0
care.

—Clayton Christensen

The ability to detect motion opens a world of possibilities for interaction and game design. Motion sensor technology can be used to create an intuitive, life-like user interface, a control mechanism in gaming, or a new kind of brush for animation.

In this chapter we will discuss how to detect motion, and we will go over some examples that make use of this information.

What Is a Motion Sensor?


Your Android device contains an onboard sensor that measures acceleration along the perpendicular axes—on the x-axis from left to right, on the y-axis from bottom to top, and on the z-axis from back to front. It calculates the forces affecting the device, both gravity and user movement. Figure 8-1 depicts acceleration measurement along the perpendicular axes on an Android device.

Figure 8-1. Measuring acceleration along the perpendicular axes on an Android device

The Accelerometer Class


The flash.sensors.Accelerometer class is an addition to ActionScript to support receiving messages from the device’s motion sensor. It is a subclass of the EventDispatcher class. The flash.events.AccelerometerEvent class is a new Event class that returns the sensor’s updated information.

When using these classes, you should first check at runtime that the user’s device has a motion sensor and that it is available for reading. Look at the value of the isSupported boolean property:

import flash.sensors.Accelerometer;

if (Accelerometer.isSupported == false) {

return;

}

The accelerometer muted property is false if the user blocks access to the accelerometer sensor. Android devices do not provide an option to disable access at the time of this writing, so you can ignore it.

Now create an instance of the Accelerometer class. This is essential to make the accelerometer variable a class variable, not a local variable, to guarantee that it is not removed from memory:

var accelerometer:Accelerometer;

accelerometer = new Accelerometer();

Next, set an AccelerometerEvent to receive updates for the device sensor:

import flash.events.AccelerometerEvent;

accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);

A timer starts ticking the moment the application is initialized and returns a timestamp value in milliseconds. This is useful if you want to average activity over time:

function onUpdate(event:AccelerometerEvent):void {

trace(event.accelerationX);

trace(event.accelerationY);

trace(event.accelerationZ);

trace(event.timestamp);

}

The event returns a floating point for each axis that ranges between ‒1.0 and 1.0. The values indicate the force pulling your device from both gravity and the user’s motion. If your device is not moving, the only acceleration measured is gravity that is, of course, active.

NOTE

Earth’s gravity is best known as the phenomenon that gives weight to objects with mass and causes them to free-fall to the ground when dropped. It is also an acceleration because a falling object keeps going faster and faster.

Acceleration is measured in meters/second2. At the surface of the Earth, acceleration is 1G (for g-force), about 9.8 m/s2 or 32 ft/s2. After one second, an object is falling at 1 G (9.8 m/s), after two seconds at 2 G (19.6 m/s), and so on, hence s2.

Motion values are not locked into the default portrait aspect ratio. If the screen orientation changes, the Accelerometer class takes care of adjusting the values so that they are set according to the stage axis, not the device axis.

Visualizing the Values


It is not very intuitive to make sense of the accelerometer values. Let’s build an application to draw the values in a way that may clarify them.

In Figure 8-2, each axis is represented, from top to bottom, by a colored bar: green for the x-axis, red for the y-axis, and blue for the z-axis. The gray vertical line in the middle of the screen represents the zero point or rest state.

Figure 8-2. The x-, y-, and z-axes, with the rest state represented by the line in the middle

Notice that if you tilt the device in a more abrupt way, values change more

Return Main Page Previous Page Next Page

®Online Book Reader