Skip to main content
Known Participant
May 13, 2013
Answered

accelerometer for my game.

  • May 13, 2013
  • 1 reply
  • 4919 views

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();

        }

    }

}

This topic has been closed for replies.
Correct answer kglad

thanks there was a space i removed it but now get these:

C:\Users\James\Desktop\v_03\com\kglad\Ship.as, Line 321067: Implicit coercion of a value of type flash.sensors:Accelerometer to an unrelated type Class.
C:\Users\James\Desktop\v_03\com\kglad\Ship.as, Line 321188: Illegal assignment to class Accelerometer.

this line:

fl_Accelerometer:Accelerometer = new Accelerometer();

should be


fl_Accelerometer = new Accelerometer();

1 reply

kglad
Community Expert
Community Expert
May 13, 2013

you use something like:

import flash.sensors.Accelerometer;

import flash.events.AccelerometerEvent;

var accelerometer:Accelerometer = new Accelerometer();

accelerometer.addEventListener(AccelerometerEvent.UPDATE,  accelerometerF,false,0,true);

function accelerometerF(e:AccelerometerEvent):void{

// do something with e based on e.accelerationX,e.accelerationY and/or accelerationZ

}

Known Participant
May 14, 2013

ok thanks i have put that all in but what would i need to put in the accelerometer function at the moment i have this that moves the ship when arrows keys pressed so would i need to delete this then what do i put in the acelerometer function?

thanks

this is the movment code at the moment:

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;

                              }

this is all the 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;

          import flash.sensors.Accelerometer;

    import flash.events.AccelerometerEvent;

 

          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;

                    var accelerometer:Accelerometer = new Accelerometer();

        accelerometer.addEventListener(AccelerometerEvent.UPDATE,  accelerometerF,false,0,true);

 

                    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);

                    }

 

                    function accelerometerF(e:AccelerometerEvent):void{

        // do something with e based on e.accelerationX,e.accelerationY and/or accelerationZ

 

        }

 

 

          }

 

}

kglad
Community Expert
Community Expert
May 14, 2013

you have to use these event properties

   

// do something with e based on e.accelerationX,e.accelerationY and/or accelerationZ

to update your object's x and y. 

use the trace function to see those property values and decide how you want to relate those values to updates of the object's x and y.