Accelerometer, AIR on Android and Tosia
Now it is fun. I did not realize that my handset has the accelerometer sensor until I saw the video. I didn’t even know that AIR for Android supports the thing. So I tried, and it worked! I modified the code from previous posts, removing multitouch events and adding simple AccelerometerEvent. This seems quite straightforward, and the only thing I still do not get is why it is called accelerometer while it aparently measures axis tilt, not acceleration.
Runtime check if the hardware supports us:
if (Accelerometer.isSupported) { accl = new Accelerometer(); accl.setRequestedUpdateInterval(200); accl.addEventListener(AccelerometerEvent.UPDATE, handleAccelerometer); } else{ tf.appendText('\naccelerometer NOT supported'); }
And the actual handler:
private function handleAccelerometer(e:AccelerometerEvent):void { var n:int = sprites.length; var s:Handle; // loop through all alive sprites to update their speed for(var i:int = 0; i < n ; i++){ s = sprites[i]; s.speedX -= e.accelerationX * 10; s.speedY += e.accelerationY * 10; } } // this exits the app on any key (event volume up/down!) private function handleKey(e:KeyboardEvent):void { NativeApplication.nativeApplication.exit(); }

