Guest User

Untitled

a guest
Oct 29th, 2013
305
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, and other stuff, you're on your own
  24. *
  25. * Terry Cavanagh
  26. * distractionware.com
  27. *
  28. * Right stick support added by Andrew Gray
  29. */
  30.  
  31. package {
  32. import flash.events.KeyboardEvent;
  33. import flash.events.Event;
  34. import flash.display.DisplayObject;
  35. import flash.ui.Keyboard;
  36. import flash.ui.GameInput;
  37. import flash.ui.GameInputControl;
  38. import flash.ui.GameInputDevice;
  39. import flash.events.GameInputEvent;
  40.  
  41. public class joypad{
  42. static private var dispObj:DisplayObject;
  43.  
  44. static public var BUTTON_O:Boolean = false;
  45. static public var BUTTON_U:Boolean = false;
  46. static public var BUTTON_Y:Boolean = false;
  47. static public var BUTTON_A:Boolean = false;
  48. static public var BUTTON_LEFT:Boolean = false;
  49. static public var BUTTON_RIGHT:Boolean = false;
  50. static public var DPAD_UP:Boolean = false;
  51. static public var DPAD_DOWN:Boolean = false;
  52. static public var DPAD_LEFT:Boolean = false;
  53. static public var DPAD_RIGHT:Boolean = false;
  54. static public var LEFT_STICK_UP:Boolean = false;
  55. static public var LEFT_STICK_DOWN:Boolean = false;
  56. static public var LEFT_STICK_LEFT:Boolean = false;
  57. static public var LEFT_STICK_RIGHT:Boolean = false;
  58. static public var RIGHT_STICK_UP:Boolean = false;
  59. static public var RIGHT_STICK_DOWN:Boolean = false;
  60. static public var RIGHT_STICK_LEFT:Boolean = false;
  61. static public var RIGHT_STICK_RIGHT:Boolean = false;
  62.  
  63. static public var control:GameInputControl;
  64.  
  65. static private var gameInput:GameInput;
  66. static private var _device:GameInputDevice;
  67.  
  68. public static function init(obj:DisplayObject){
  69. dispObj = obj;
  70. dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
  71.  
  72. gameInput = new GameInput();
  73. gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, handleDeviceAttached);
  74. gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, handleDeviceRemoved);
  75. }
  76.  
  77. protected static function handleDeviceRemoved(event:GameInputEvent):void
  78. {
  79. }
  80.  
  81. public static function anybutton():Boolean {
  82. if (BUTTON_O || BUTTON_U || BUTTON_Y || BUTTON_A) {
  83. return true;
  84. }
  85. return false;
  86. }
  87.  
  88. public static function pressup():Boolean {
  89. if (LEFT_STICK_UP || DPAD_UP) return true;
  90. return false;
  91. }
  92.  
  93. public static function pressdown():Boolean {
  94. if (LEFT_STICK_DOWN || DPAD_DOWN) return true;
  95. return false;
  96. }
  97.  
  98. public static function pressleft():Boolean {
  99. if (LEFT_STICK_LEFT || DPAD_LEFT) return true;
  100. return false;
  101. }
  102.  
  103. public static function pressright():Boolean {
  104. if (LEFT_STICK_RIGHT || DPAD_RIGHT) return true;
  105. return false;
  106. }
  107.  
  108. protected static function handleDeviceAttached(e:GameInputEvent):void{
  109. GameInputControlName.initialize(e.device);
  110.  
  111. var i:int;
  112.  
  113. for(var k:Number=0;k<GameInput.numDevices;k++){
  114. _device = GameInput.getDeviceAt(k);
  115. var _controls:Vector.<String> = new Vector.<String>;
  116. _device.enabled = true;
  117.  
  118. for (i = 0; i < _device.numControls; i++) {
  119. control = _device.getControlAt(i);
  120. _controls[i] = control.id;
  121.  
  122. if(control.id == "AXIS_0") control.addEventListener(Event.CHANGE, leftxaxisChangeHandler);
  123. if(control.id == "AXIS_1") control.addEventListener(Event.CHANGE, leftyaxisChangeHandler);
  124. if(control.id == "AXIS_11") control.addEventListener(Event.CHANGE, rightxaxisChangeHandler);
  125. if(control.id == "AXIS_14") control.addEventListener(Event.CHANGE, rightyaxisChangeHandler);
  126. if(control.id == "BUTTON_96") control.addEventListener(Event.CHANGE, buttonOChangeHandler);
  127. if(control.id == "BUTTON_97") control.addEventListener(Event.CHANGE, buttonAChangeHandler);
  128. if(control.id == "BUTTON_99") control.addEventListener(Event.CHANGE, buttonUChangeHandler);
  129. if(control.id == "BUTTON_100") control.addEventListener(Event.CHANGE, buttonYChangeHandler);
  130. if(control.id == "BUTTON_102") control.addEventListener(Event.CHANGE, buttonlefttriggerChangeHandler);
  131. if(control.id == "BUTTON_103") control.addEventListener(Event.CHANGE, buttonrighttriggerChangeHandler);
  132. if(control.id == "BUTTON_19") control.addEventListener(Event.CHANGE, buttondpadupChangeHandler);
  133. if(control.id == "BUTTON_20") control.addEventListener(Event.CHANGE, buttondpaddownChangeHandler);
  134. if(control.id == "BUTTON_21") control.addEventListener(Event.CHANGE, buttondpadleftChangeHandler);
  135. if(control.id == "BUTTON_22") control.addEventListener(Event.CHANGE, buttondpadrightChangeHandler);
  136. }
  137. }
  138. }
  139.  
  140. public static function buttondpadupChangeHandler(e:Event):void {
  141. control = e.target as GameInputControl;
  142. if (control.value == 1) DPAD_UP = true;
  143. if (control.value == 0) DPAD_UP = false;
  144. }
  145.  
  146. public static function buttondpaddownChangeHandler(e:Event):void{
  147. control = e.target as GameInputControl;
  148. if (control.value == 1) DPAD_DOWN = true;
  149. if (control.value == 0) DPAD_DOWN = false;
  150. }
  151.  
  152. public static function buttondpadleftChangeHandler(e:Event):void{
  153. control = e.target as GameInputControl;
  154. if (control.value == 1) DPAD_LEFT = true;
  155. if (control.value == 0) DPAD_LEFT = false;
  156. }
  157.  
  158. public static function buttondpadrightChangeHandler(e:Event):void{
  159. control = e.target as GameInputControl;
  160. if (control.value == 1) DPAD_RIGHT = true;
  161. if (control.value == 0) DPAD_RIGHT = false;
  162. }
  163.  
  164. public static function buttonlefttriggerChangeHandler(e:Event):void{
  165. control = e.target as GameInputControl;
  166. if (control.value == 1) BUTTON_LEFT = true;
  167. if (control.value == 0) BUTTON_LEFT = false;
  168. }
  169.  
  170. public static function buttonrighttriggerChangeHandler(e:Event):void{
  171. control = e.target as GameInputControl;
  172. if (control.value == 1) BUTTON_RIGHT = true;
  173. if (control.value == 0) BUTTON_RIGHT = false;
  174. }
  175.  
  176. public static function buttonOChangeHandler(e:Event):void{
  177. control = e.target as GameInputControl;
  178. if (control.value == 1) BUTTON_O = true;
  179. if (control.value == 0) BUTTON_O = false;
  180. }
  181.  
  182. public static function buttonUChangeHandler(e:Event):void{
  183. control = e.target as GameInputControl;
  184. if (control.value == 1) BUTTON_U = true;
  185. if (control.value == 0) BUTTON_U = false;
  186. }
  187.  
  188. public static function buttonYChangeHandler(e:Event):void{
  189. control = e.target as GameInputControl;
  190. if (control.value == 1) BUTTON_Y = true;
  191. if (control.value == 0) BUTTON_Y = false;
  192. }
  193.  
  194. public static function buttonAChangeHandler(e:Event):void{
  195. control = e.target as GameInputControl;
  196. if (control.value == 1) BUTTON_A = true;
  197. if (control.value == 0) BUTTON_A = false;
  198. }
  199.  
  200. public static function leftxaxisChangeHandler(e:Event):void{
  201. control = e.target as GameInputControl;
  202. if (control.value < -0.5) {
  203. LEFT_STICK_LEFT = true;
  204. LEFT_STICK_RIGHT = false;
  205. }else if (control.value > 0.5) {
  206. LEFT_STICK_LEFT = false;
  207. LEFT_STICK_RIGHT = true;
  208. }else {
  209. LEFT_STICK_LEFT = false;
  210. LEFT_STICK_RIGHT = false;
  211. }
  212. }
  213.  
  214. public static function leftyaxisChangeHandler(e:Event):void{
  215. control = e.target as GameInputControl;
  216. if (control.value < -0.5) {
  217. LEFT_STICK_UP = true;
  218. LEFT_STICK_DOWN = false;
  219. }else if (control.value > 0.5) {
  220. LEFT_STICK_UP = false;
  221. LEFT_STICK_DOWN = true;
  222. }else {
  223. LEFT_STICK_UP = false;
  224. LEFT_STICK_DOWN = false;
  225. }
  226. }
  227.  
  228. public static function rightxaxisChangeHandler(e:Event):void{
  229. control = e.target as GameInputControl;
  230. if (control.value < -0.5) {
  231. RIGHT_STICK_LEFT = true;
  232. RIGHT_STICK_RIGHT = false;
  233. }else if (control.value > 0.5) {
  234. RIGHT_STICK_LEFT = false;
  235. RIGHT_STICK_RIGHT = true;
  236. }else {
  237. RIGHT_STICK_LEFT = false;
  238. RIGHT_STICK_RIGHT = false;
  239. }
  240. }
  241.  
  242. public static function rightyaxisChangeHandler(e:Event):void{
  243. control = e.target as GameInputControl;
  244. if (control.value < -0.5) {
  245. RIGHT_STICK_UP = true;
  246. RIGHT_STICK_DOWN = false;
  247. }else if (control.value > 0.5) {
  248. RIGHT_STICK_UP = false;
  249. RIGHT_STICK_DOWN = true;
  250. }else {
  251. RIGHT_STICK_UP = false;
  252. RIGHT_STICK_DOWN = false;
  253. }
  254. }
  255.  
  256. private static function keyDownListener( ev:KeyboardEvent ):void {
  257. if (ev.keyCode == 27) ev.preventDefault();
  258. if (ev.keyCode==Keyboard.BACK) ev.preventDefault();
  259. }
  260. }
  261. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×