我希望有人幫助我將此代碼從使用鍵盤轉換爲使用Android設備加速計。因此,如果設備向Y軸傾斜,則應用程序應迴應使對象向前移動,如果設備傾斜至X軸或-X軸,應用程序應將對象向左或向右旋轉。Flash Cs5.5 Android AIR - 將代碼從鍵盤轉換爲加速度計
任何人都可以幫忙嗎?
我的代碼:
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;
}
}
}