Advertisement
Guest User

ArrowKeyMovement

a guest
Jul 7th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  libTF.interfaces
  2. {
  3.     import flash.display.Shape;
  4.     import flash.events.EventDispatcher;
  5.     import flash.events.Event;
  6.     import flash.events.IEventDispatcher;
  7.     import flash.geom.Rectangle;
  8.     import flash.ui.Keyboard;
  9.    
  10.     public class ArrowKeyMovement
  11.     {
  12.         public var friction:Number;
  13.         public var speed:int;
  14.         public var x:int, y:int;
  15.         public var xVel:Number, yVel:Number;
  16.         public var bounds:Rectangle;
  17.         private var keyBind:int;
  18.        
  19.         public function ArrowKeyMovement(spd:int, bnd:Rectangle = null, frict:Number = 1, arrows:Boolean = true, wasd:Boolean = false)
  20.         {
  21.             speed = spd;
  22.             bounds = bnd;
  23.             friction = frict;
  24.             x = y = 0;
  25.             xVel = yVel = 0;
  26.             keyBind = int(arrows) + (int(wasd) << 1);
  27.             keyBind = (!keyBind)?1:keyBind;
  28.             (new Shape()).addEventListener(Event.ENTER_FRAME, move);
  29.         }
  30.        
  31.         public function useArrows():void {keyBind = 1;}
  32.         public function useWASD():void {keyBind = 2;}
  33.         public function useBoth():void {keyBind = 3;}
  34.        
  35.         private function move(e:Event):void
  36.         {
  37.             xVel *= (1 - friction);
  38.             yVel *= (1 - friction);
  39.             if (keyBind == 1) { handleArrows(); }
  40.             else if (keyBind == 2) { handleWASD(); }
  41.             else if (keyBind == 3) { handleBoth(); }
  42.             x += xVel;
  43.             y += yVel;
  44.             if (bounds)
  45.             {
  46.                 if (x < bounds.x) { x = bounds.x; xVel = 0; }
  47.                 else if (x > bounds.x + bounds.width) { x = bounds.x + bounds.width;  xVel = 0; }
  48.                 if (y < bounds.y) { y = bounds.y; yVel = 0; }
  49.                 else if (y > bounds.y + bounds.height) { y = bounds.y + bounds.height; yVel = 0; }
  50.             }
  51.         }
  52.        
  53.         private function handleWASD():void
  54.         {
  55.             if (KeyManager.isPressed(65)) { xVel -= speed; }
  56.             if (KeyManager.isPressed(87)) { yVel -= speed; }
  57.             if (KeyManager.isPressed(68)) { xVel += speed; }
  58.             if (KeyManager.isPressed(83)) { yVel += speed; }
  59.         }
  60.        
  61.         private function handleArrows():void
  62.         {
  63.             if (KeyManager.isPressed(37)) { xVel -= speed; }
  64.             if (KeyManager.isPressed(38)) { yVel -= speed; }
  65.             if (KeyManager.isPressed(39)) { xVel += speed; }
  66.             if (KeyManager.isPressed(40)) { yVel += speed; }
  67.         }
  68.         private function handleBoth():void
  69.         {
  70.             if (KeyManager.isPressed(37) || KeyManager.isPressed(65)) { xVel -= speed; }
  71.             if (KeyManager.isPressed(38) || KeyManager.isPressed(87)) { yVel -= speed; }
  72.             if (KeyManager.isPressed(39) || KeyManager.isPressed(68)) { xVel += speed; }
  73.             if (KeyManager.isPressed(40) || KeyManager.isPressed(83)) { yVel += speed; }
  74.         }
  75.        
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement