Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [44]

By Root 2491 0
30);

ball.graphics.beginFill(0x3300FF);

ball.graphics.drawCircle(10,10, 10);

ball.graphics.endFill();

ball.x = stage.stageWidth/2;

ball.y = stage.stageHeight/2;

ball.cacheAsBitmap = true;

addChild(ball);

function onEnterFrame(event:Event):void {

event.stopPropagation();

newX = ball.x - vx;

newY = ball.y + vy;

if (newX > radius && newX < xBounds) {

ball.x = newX;

}

if (newY > radius && newY < yBounds) {

ball.y = newY;

}

var dx:int = centerX - ball.x;

var dy:int = centerY - ball.y;

var radians:Number = Math.atan2(dy, dx);

ball.rotation = radians*180/Math.PI;

}

Shake Me


Shaking the device is a novel and yet intuitive interaction. You can use it to detect the user’s strength, to simulate a real object as in the game Ask the Magic Eight Ball, or simply as a reset button.

Shaking can be defined as a drastic motion, and therefore as a relatively large change of value over time. To detect the force of shaking, you can compare acceleration values against a threshold regardless of the orientation. In this example, if a value on any of the axes goes above the threshold of 1.5, the motion is considered a shake:

const THRESHOLD:Number = 1.5;

function onUpdate(event:AccelerometerEvent):void {

if (event.accelerationX > THRESHOLD || event.accelerationY > THRESHOLD

|| event.accelerationZ > THRESHOLD ) {

trace("strong enough");

var max:Number =

Math.max(event.accelerationX, event.accelerationY,

event.accelerationZ);

trace("you are ", max.toString(), "strong");

}

}

To detect an actual shaking motion, vary the condition for each coordinate and check the value in both directions. Generally, shaking happens more along the x-axis, happens somewhat less along the y-axis, and is relatively insignificant along the z-axis. This is because a natural human motion is to move one’s arm from left to right. To reflect this, the threshold for x is 2.5. The threshold for y is twice that value, and the threshold for z is three times that value.

The boolean isMeasuring allows for clean reading of all the axes together. It is set to true when it starts to capture values and false only after all the accelerometer axes have been captured:

const THRESHOLD:Number = 2.5;

var isMeasuring:Boolean = false;

var isShaking:Boolean = false;

var accelerometer:Accelerometer;

accelerometer = new Accelerometer();

accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);

function onUpdate(event:AccelerometerEvent):void {

if (isMeasuring) {

return;

}

isMeasuring = true;

if (Math.abs(event.accelerationX) > THRESHOLD) {

isShaking = true;

}

if (Math.abs(event.accelerationY) > THRESHOLD*2) {

isShaking = true;

}

if (Math.abs(event.accelerationZ) > THRESHOLD*3) {

isShaking = true;

}

if (isShaking) {

// we have a shake

}

isMeasuring = false;

}

Using the same kind of analysis, you could imagine capturing other device gestures, such as a circle or a zigzag motion. For such motion, though, timing would need to be incorporated.

Smoothing Out Values


Accelerometer sensors are prone to spurious results. Accelerometer values are not always coming across at a regular pace, and users can make erratic movements. If you want to avoid a jittery effect, smooth out the values. You can do this by combining previous readings with the current reading and leaving out a range of values according to a defined filter.

There are two approaches to accomplishing this. A high-pass filter is used for an application that is looking for rapid changes by weighting the current reading more than the previous reading. A good use case for a high-pass filter is detecting a shake motion, as in our previous example. A low-pass filter is used for an application that is looking for fine-tuned, stable values. This type of filter would be used in a game of precision, such as carefully pushing an object toward a target.

When testing these filters, the values start adjusting as expected after a few seconds.

Using a high-pass filter


The high-pass filter works with a fixed filter. Try different filter values until you

Return Main Page Previous Page Next Page

®Online Book Reader