Rotating Toward the Center, Shake Me

Must Read

Criminal Defense Attorney Advice On How To Avoid Improper Student Teacher Relationship

Sexual wrong doings and sexual maltreatment of minors within the primary school system is no longer news. Despite our...

Converting Loans Into Fixed Rate Student Loans

The only fixed rate student loans available are federal loans, and even those can change based on federal law....

Bad Credit Home Loan Refinance Mortgage Online

Just because you have bad credit doesn't mean you can't get a refinance home loan. In fact, it has...

In this example, the ball turns toward the center of the screen while it is moving. Making an element react to its environment brings it to life.

With the following code, the larger ball now contains a small ball that is off-centered so that the rotation is visible:

var ball:Shape;
var centerX:int = stage.stageWidth/2;
var centerY:int = stage.stageHeight/2;
var newX:Number = 0.0;
var newY:Number = 0.0;
ball = new Shape();;,0, 30);;,10, 10);;
ball.x = stage.stageWidth/2;
ball.y = stage.stageHeight/2;
ball.cacheAsBitmap = true;
function onEnterFrame(event:Event):void {
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,
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) {
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


Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This