2013-04-04 38 views
0

我希望有人幫助我將此代碼從使用鍵盤轉換爲使用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; 
    } 

} 

}

回答

0

您可以使用dispatchEvent方法,編輯事件的屬性,如類型和加速度。我沒有Android設備,因此不能說下面的代碼將100%正常工作。

var ev:AccelerometerEvent = new AccelerometerEvent(AccelerometerEvent.UPDATE); 
ev.accelerationX = yourKeyboardAccelerationX; 
ev.accelerationX = yourKeyboardAccelerationY; 
dispatchEvent(ev); 
0

你可以用e.accelerationX,e.accelerationY和e.accelerationX玩:

import flash.sensors.Accelerometer; 
import flash.events.AccelerometerEvent; 
var my_acc:Accelerometer 
if(Accelerometer.isSupported) 
    initAcc(); 
else 
trace("Accelerometer is not Supported") 

function initAcc():void 
{ 
var my_acc:Accelerometer = new Accelerometer(); 
//setting the update interval 
my_acc.setRequestedUpdateInterval(50); 

//catch the update event 
my_acc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate); 

} 
function onAccUpdate(e:AccelerometerEvent):void{ 
//you can use the e.accelerationX,e.accelerationY AND e.accelerationX 

myObject.x -= (e.accelerationX*10); 
myObject.y += (e.accelerationY*10); 

}