Sunday, 5 December 2010

KeyObject class in Actionscript 3

In Actionscript 3 the only way you can react to keyboard presses is via the KeyDown and KeyUp events. However, Senocular has created the KeyObect class which has the key.isDown() and key.isUp() methods from Actionscript 2. Here is a simple example.

var speed:uint = 5;

var key:KeyObject = new KeyObject(stage);
addEventListener( Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void {
   if(key.isDown(Keyboard.LEFT)){
      lemon_mc.x -= speed; 
   }

   if(key.isDown(Keyboard.RIGHT)){
      lemon_mc.x += speed; 
   } 

   if(key.isDown(Keyboard.UP)){
      lemon_mc.y -= speed; 
   }

   if(key.isDown(Keyboard.DOWN)){
      lemon_mc.y += speed; 
   }
}

I have also used the code from the limit stage boundaries tutorial to stop the object at the edges of the stage.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP