Advertisement
ulfben

Key substitute for AS3

Jan 14th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Key
  2.     {
  3.         import flash.display.Stage;
  4.         import flash.events.Event;
  5.         import flash.events.KeyboardEvent;
  6.            
  7.         private static var initialized:Boolean = false;    
  8.         private static var keysDown:Object = new Object();
  9.        
  10.         //player controlls
  11.         public static const USE:uint = 88;
  12.         public static const SHOOT:uint = 32;
  13.         public static const SHOOT2:uint = 40;
  14.         public static const ACCELERATE:uint = 38; //TODO: set all these with XML!
  15.         public static const LEFT:uint = 37;
  16.         public static const RIGHT:uint = 39;
  17.    
  18.        
  19.         /**
  20.          * Initializes the key class creating assigning event
  21.          * handlers to capture necessary key events from the stage
  22.          */
  23.         public static function initialize(stage:Stage) {
  24.             if (!initialized) {
  25.                 // assign listeners for key presses and deactivation of the player
  26.                 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  27.                 stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
  28.                 stage.addEventListener(Event.DEACTIVATE, clearKeys);               
  29.                 // mark initialization as true so redundant
  30.                 // calls do not reassign the event handlers
  31.                 initialized = true;
  32.             }
  33.         }
  34.        
  35.         /**
  36.          * Returns true or false if the key represented by the
  37.          * keyCode passed is being pressed
  38.          */
  39.         public static function isDown(keyCode:uint):Boolean {
  40.             if (!initialized) {
  41.                 // throw an error if isDown is used
  42.                 // prior to Key class initialization
  43.                 throw new Error("Key class has not been initialized.");
  44.             }
  45.             return Boolean(keyCode in keysDown);
  46.         }
  47.        
  48.         /**
  49.          * Event handler for capturing keys being pressed
  50.          */
  51.         private static function keyPressed(event:KeyboardEvent):void {
  52.             // create a property in keysDown with the name of the keyCode
  53.             //trace(event.keyCode);
  54.             keysDown[event.keyCode] = true;
  55.         }
  56.        
  57.         /**
  58.          * Event handler for capturing keys being released
  59.          */
  60.         private static function keyReleased(event:KeyboardEvent):void {
  61.             if (event.keyCode in keysDown) {
  62.                 // delete the property in keysDown if it exists
  63.                 delete keysDown[event.keyCode];
  64.             }
  65.         }
  66.        
  67.         /**
  68.          * Event handler for Flash Player deactivation
  69.          */
  70.         private static function clearKeys(event:Event):void {
  71.             // clear all keys in keysDown since the player cannot
  72.             // detect keys being pressed or released when not focused
  73.             keysDown = new Object();
  74.         }  
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement