accelerometer for my game.
hi
im making a flash game and trying out a few things in it but what i really want is for my main character the ship to move when you tilt the device at the moment you just use the arrow keys. can any one tell me what i neeed to do to turn my ship control from using arrows keys to an ios accelerometer?
thanks
ship code:
package com.kglad{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.filters.GlowFilter;
public class Ship extends MovieClip{
var velocity:Number;
var shootLimiter:Number;
var health:Number;
var maxHealth:Number;
public var xBuffer:int;
public var yBuffer:int;
private var _stage:Stage
private var gf:GlowFilter;
private var gfColorA:Array = [0x0000ff,0xff0000];
public var shieldINT:int;
public function Ship(){
this.addEventListener(Event.ADDED_TO_STAGE,init);
this.addEventListener(Event.REMOVED_FROM_STAGE,removedF);
}
function init(e:Event):void{
shieldINT = 0;
gf = new GlowFilter();
gf.color = gfColorA[Data.shipFrame-1];
if(Data.shipFrame!=1){
this.gotoAndStop(Data.shipFrame);
}
_stage = stage;
velocity = 10;
shootLimiter = 0;
maxHealth = 100;
health = maxHealth;
if(this.name!="shopShip"){
addEventListener("enterFrame", move);
}
}
function kill(){
var explosion = new Explosion();
_stage.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
removeEventListener("enterFrame", move);
this.visible = false;
Game.gameOver();
}
function takeDamage(d){
health -= d;
if(health<=0){
health = 0;
kill();
}
Game.healthMeter.bar.scaleX = health/maxHealth;
}
function move(e:Event){
shootLimiter += 1;
if(KeyClass.isDown(Keyboard.RIGHT)){
this.x = this.x + velocity;
}
if(KeyClass.isDown(Keyboard.LEFT)){
this.x = this.x - velocity;
}
if(KeyClass.isDown(Keyboard.UP)){
this.y = this.y - velocity;
}
if(KeyClass.isDown(Keyboard.DOWN)){
this.y = this.y + velocity;
}
if(KeyClass.isDown(Keyboard.SPACE) && shootLimiter > 8){
shootLimiter = 0;
var b = new Bullet();
_stage.addChild(b);
b.x = this.x + 50;
b.y = this.y + 3;
}
if(shieldINT>0){
gf.blurX = gf.blurY = 4*shieldINT;
this.filters = [gf];
} else {
this.filters = [];
}
/*
if(shield.visible == true){
shield.alpha -= 0.0005;
if(shield.alpha == 0){
shield.visible = false;
shield.alpha = 1;
}
}
*/
}
private function removedF(e:Event):void{
removeEventListener("enterFrame", move);
}
}
}
i also have a key class:
/*
This class get initialized in the Constructor of the Game class.
This class manages basic key presses, user input.
You can just use this class by calling: KeyClass.isDown(Keyboard.LEFT) from other classes like in AS2.0.
*/
package com.kglad{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class KeyClass {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object(); // stores key codes of all keys pressed
public static function initialize(_stage:Stage) {
if (!initialized) {
// assign listeners for key presses and deactivation of the player
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
_stage.addEventListener(Event.DEACTIVATE, clearKeys);
// mark initialization as true so redundant
// calls do not reassign the event handlers
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean {
//return Boolean(keyCode in keysDown);
return keysDown[keyCode];
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
keysDown[event.keyCode] = false;
//if (event.keyCode in keysDown) {
//delete keysDown[event.keyCode];
//}
}
private static function clearKeys(event:Event):void {
// clear all keys in keysDown since the player cannot detect keys being pressed or released when not focused
keysDown = new Object();
}
}
}
