Advertisement
Guest User

Untitled

a guest
Jun 27th, 2013
1,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Simple OUYA joypad class
  3.  * Based on Adobe's official joypad examples
  4.  *
  5.  * Only works for one player, all attached joypads control the game
  6.  *
  7.  * HOW TO USE:
  8.  * This is a static class, so it can be accessed from anywhere in your code.
  9.  * Somewhere in your init function, call:
  10.  *
  11.  * joypad.init(stage);
  12.  *
  13.  * to set up the event listeners. Then anywhere else in code, you can check the following
  14.  * boolean variables
  15.  * joypad.STICK_LEFT (or RIGHT, UP, DOWN)
  16.  * joypad.DPAD_LEFT (etc)
  17.  * joypad.BUTTON_O (or U, Y, A, LEFT or RIGHT)
  18.  *
  19.  * There are also shortcut functions:
  20.  * joypad.anybutton() (checks O, U, Y and A buttons)
  21.  * joypad.pressup() (checks both stick up and dpad up, pressleft/right/down also work)
  22.  *
  23.  * For triggers, right stick, and other stuff, you're on your own
  24.  *
  25.  * Terry Cavanagh
  26.  * distractionware.com
  27.  *
  28.  */
  29.  
  30. package {
  31.     import flash.events.KeyboardEvent;
  32.     import flash.events.Event;
  33.     import flash.display.DisplayObject;
  34.     import flash.ui.Keyboard;
  35.     import flash.ui.GameInput;
  36.     import flash.ui.GameInputControl;
  37.     import flash.ui.GameInputDevice;
  38.     import flash.events.GameInputEvent;
  39.    
  40.     public class joypad{
  41.         static private var dispObj:DisplayObject;
  42.        
  43.         static public var BUTTON_O:Boolean = false;
  44.         static public var BUTTON_U:Boolean = false;
  45.         static public var BUTTON_Y:Boolean = false;
  46.         static public var BUTTON_A:Boolean = false;
  47.         static public var BUTTON_LEFT:Boolean = false;
  48.         static public var BUTTON_RIGHT:Boolean = false;
  49.         static public var DPAD_UP:Boolean = false;
  50.         static public var DPAD_DOWN:Boolean = false;
  51.         static public var DPAD_LEFT:Boolean = false;
  52.         static public var DPAD_RIGHT:Boolean = false;
  53.         static public var STICK_UP:Boolean = false;
  54.         static public var STICK_DOWN:Boolean = false;
  55.         static public var STICK_LEFT:Boolean = false;
  56.         static public var STICK_RIGHT:Boolean = false;
  57.        
  58.         static public var control:GameInputControl;
  59.        
  60.         static private var gameInput:GameInput;
  61.         static private var _device:GameInputDevice;
  62.        
  63.         public static function init(obj:DisplayObject){
  64.             dispObj = obj;
  65.             dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
  66.            
  67.             gameInput = new GameInput();
  68.             gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, handleDeviceAttached);
  69.             gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, handleDeviceRemoved);
  70.         }
  71.        
  72.         protected static function handleDeviceRemoved(event:GameInputEvent):void
  73.         {
  74.         }
  75.        
  76.         public static function anybutton():Boolean {
  77.             if (BUTTON_O || BUTTON_U || BUTTON_Y || BUTTON_A) {
  78.                 return true;
  79.             }
  80.             return false;
  81.         }
  82.        
  83.         public static function pressup():Boolean {
  84.             if (STICK_UP || DPAD_UP) return true;
  85.             return false;
  86.         }
  87.        
  88.         public static function pressdown():Boolean {
  89.             if (STICK_DOWN || DPAD_DOWN) return true;
  90.             return false;
  91.         }
  92.        
  93.         public static function pressleft():Boolean {
  94.             if (STICK_LEFT || DPAD_LEFT) return true;
  95.             return false;
  96.         }
  97.        
  98.         public static function pressright():Boolean {
  99.             if (STICK_RIGHT || DPAD_RIGHT) return true;
  100.             return false;
  101.         }
  102.        
  103.         protected static function handleDeviceAttached(e:GameInputEvent):void{
  104.             GameInputControlName.initialize(e.device);
  105.            
  106.             var i:int;
  107.            
  108.             for(var k:Number=0;k<GameInput.numDevices;k++){
  109.                 _device = GameInput.getDeviceAt(k);
  110.                 var _controls:Vector.<String> = new Vector.<String>;
  111.                 _device.enabled = true;
  112.                
  113.                 for (i = 0; i < _device.numControls; i++) {
  114.                     control = _device.getControlAt(i);
  115.                     _controls[i] = control.id;
  116.                    
  117.                     if(control.id == "AXIS_0") control.addEventListener(Event.CHANGE, xaxisChangeHandler);
  118.                     if(control.id == "AXIS_1") control.addEventListener(Event.CHANGE, yaxisChangeHandler);
  119.                     if(control.id == "BUTTON_96") control.addEventListener(Event.CHANGE, buttonOChangeHandler);
  120.                     if(control.id == "BUTTON_97") control.addEventListener(Event.CHANGE, buttonAChangeHandler);
  121.                     if(control.id == "BUTTON_99") control.addEventListener(Event.CHANGE, buttonUChangeHandler);
  122.                     if(control.id == "BUTTON_100") control.addEventListener(Event.CHANGE, buttonYChangeHandler);
  123.                     if(control.id == "BUTTON_102") control.addEventListener(Event.CHANGE, buttonlefttriggerChangeHandler);
  124.                     if(control.id == "BUTTON_103") control.addEventListener(Event.CHANGE, buttonrighttriggerChangeHandler);
  125.                     if(control.id == "BUTTON_19")   control.addEventListener(Event.CHANGE, buttondpadupChangeHandler);
  126.                     if(control.id == "BUTTON_20")   control.addEventListener(Event.CHANGE, buttondpaddownChangeHandler);
  127.                     if(control.id == "BUTTON_21")   control.addEventListener(Event.CHANGE, buttondpadleftChangeHandler);
  128.                     if(control.id == "BUTTON_22") control.addEventListener(Event.CHANGE, buttondpadrightChangeHandler);
  129.                 }
  130.             }
  131.         }  
  132.        
  133.         public static function buttondpadupChangeHandler(e:Event):void {
  134.             control = e.target as GameInputControl;
  135.             if (control.value == 1) DPAD_UP = true;
  136.             if (control.value == 0) DPAD_UP = false;
  137.         }
  138.        
  139.         public static function buttondpaddownChangeHandler(e:Event):void{
  140.             control = e.target as GameInputControl;
  141.             if (control.value == 1) DPAD_DOWN = true;
  142.             if (control.value == 0) DPAD_DOWN = false;
  143.         }
  144.        
  145.         public static function buttondpadleftChangeHandler(e:Event):void{
  146.             control = e.target as GameInputControl;
  147.             if (control.value == 1) DPAD_LEFT = true;
  148.             if (control.value == 0) DPAD_LEFT = false;
  149.         }
  150.        
  151.         public static function buttondpadrightChangeHandler(e:Event):void{
  152.             control = e.target as GameInputControl;
  153.             if (control.value == 1) DPAD_RIGHT = true;
  154.             if (control.value == 0) DPAD_RIGHT = false;
  155.         }
  156.        
  157.         public static function buttonlefttriggerChangeHandler(e:Event):void{
  158.             control = e.target as GameInputControl;
  159.             if (control.value == 1) BUTTON_LEFT = true;
  160.             if (control.value == 0) BUTTON_LEFT = false;
  161.         }
  162.        
  163.         public static function buttonrighttriggerChangeHandler(e:Event):void{
  164.             control = e.target as GameInputControl;
  165.             if (control.value == 1) BUTTON_RIGHT = true;
  166.             if (control.value == 0) BUTTON_RIGHT = false;
  167.         }
  168.        
  169.         public static function buttonOChangeHandler(e:Event):void{
  170.             control = e.target as GameInputControl;
  171.             if (control.value == 1) BUTTON_O = true;
  172.             if (control.value == 0) BUTTON_O = false;
  173.         }
  174.        
  175.         public static function buttonUChangeHandler(e:Event):void{
  176.             control = e.target as GameInputControl;
  177.             if (control.value == 1) BUTTON_U = true;
  178.             if (control.value == 0) BUTTON_U = false;
  179.         }
  180.        
  181.         public static function buttonYChangeHandler(e:Event):void{
  182.             control = e.target as GameInputControl;
  183.             if (control.value == 1) BUTTON_Y = true;
  184.             if (control.value == 0) BUTTON_Y = false;
  185.         }
  186.        
  187.         public static function buttonAChangeHandler(e:Event):void{
  188.             control = e.target as GameInputControl;
  189.             if (control.value == 1) BUTTON_A = true;
  190.             if (control.value == 0) BUTTON_A = false;
  191.         }
  192.        
  193.         public static function xaxisChangeHandler(e:Event):void{
  194.             control = e.target as GameInputControl;
  195.             if (control.value < -0.5) {
  196.                 STICK_LEFT = true;
  197.                 STICK_RIGHT = false;
  198.             }else if (control.value > 0.5) {
  199.                 STICK_LEFT = false;
  200.                 STICK_RIGHT = true;
  201.             }else {
  202.                 STICK_LEFT = false;
  203.                 STICK_RIGHT = false;
  204.             }
  205.         }
  206.        
  207.         public static function yaxisChangeHandler(e:Event):void{
  208.             control = e.target as GameInputControl;
  209.             if (control.value < -0.5) {
  210.                 STICK_UP = true;
  211.                 STICK_DOWN = false;
  212.             }else if (control.value > 0.5) {
  213.                 STICK_UP = false;
  214.                 STICK_DOWN = true;
  215.             }else {
  216.                 STICK_UP = false;
  217.                 STICK_DOWN = false;
  218.             }
  219.         }
  220.        
  221.         private static function keyDownListener( ev:KeyboardEvent ):void    {
  222.             if (ev.keyCode == 27)   ev.preventDefault();
  223.             if (ev.keyCode==Keyboard.BACK) ev.preventDefault();
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement