I'd like someone to help me convert this code from using a keyboard to using an android device accelerometer. So if the device is tilted to Y axis the application should respond back making the object move forward and if the device is tilted to X axis or -X axis the application should rotate the object to left or right.
Can anyone help?
My code:
package com.asgamer.directionalmovement
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
public class Ship extends MovieClip
{
private var key:KeyObject;
private var speed:Number = 0.3;
private var rotateSpeed:Number = 5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.95;
public function Ship () : void
{
key = new KeyObject(stage);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (key.isDown(Keyboard.UP))
{
vy += Math.sin(degreesToRadians(rotation)) * speed;
vx += Math.cos(degreesToRadians(rotation)) * speed;
} else {
vy *= friction;
vx *= friction;
}
if (key.isDown(Keyboard.RIGHT))
rotation += rotateSpeed;
else if (key.isDown(Keyboard.LEFT))
rotation -= rotateSpeed;
if (key.isDown(Keyboard.SPACE))
stage.addChild(new Laser(x, y, rotation));
y += vy;
x += vx;
if (x > stage.stageWidth)
x = 0;
else if (x < 0)
x = stage.stageWidth;
if (y > stage.stageHeight)
y = 0;
else if (y < 0)
y = stage.stageHeight;
}
public function degreesToRadians(degrees:Number) : Number
{
return degrees * Math.PI / 180;
}
}
}