├── logo.gif ├── logo.png ├── readme.txt ├── logo_small.gif ├── logo_small.png ├── GamepadTester.fla ├── GamepadTester.swf ├── GamepadTester_CS3.fla ├── PlatformGamePadTester.fla ├── PlatformGamePadTester.swf ├── PlatformGamePadTester_CS3.fla ├── MIT-LICENSE.txt ├── GAMEPAD.as3proj └── com ├── iainlobb ├── gamepadtesters │ ├── PlatformGamePadTester.as │ ├── PlatformGameCharacter.as │ └── GamePadTester.as └── gamepad │ ├── GamepadMultiInput.as │ ├── GamepadInput.as │ ├── GamepadView.as │ └── Gamepad.as └── cheezeworld └── utils └── KeyCode.as /logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/logo.gif -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/logo.png -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/readme.txt -------------------------------------------------------------------------------- /logo_small.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/logo_small.gif -------------------------------------------------------------------------------- /logo_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/logo_small.png -------------------------------------------------------------------------------- /GamepadTester.fla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/GamepadTester.fla -------------------------------------------------------------------------------- /GamepadTester.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/GamepadTester.swf -------------------------------------------------------------------------------- /GamepadTester_CS3.fla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/GamepadTester_CS3.fla -------------------------------------------------------------------------------- /PlatformGamePadTester.fla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/PlatformGamePadTester.fla -------------------------------------------------------------------------------- /PlatformGamePadTester.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/PlatformGamePadTester.swf -------------------------------------------------------------------------------- /PlatformGamePadTester_CS3.fla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iainlobb/Gamepad/HEAD/PlatformGamePadTester_CS3.fla -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Iain Lobb 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /GAMEPAD.as3proj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 84 | -------------------------------------------------------------------------------- /com/iainlobb/gamepadtesters/PlatformGamePadTester.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepadtesters 27 | { 28 | import com.cheezeworld.utils.KeyCode; 29 | import com.iainlobb.gamepad.Gamepad; 30 | import com.iainlobb.gamepad.GamepadView; 31 | import flash.display.MovieClip; 32 | import flash.display.Sprite; 33 | import flash.events.Event; 34 | 35 | /** 36 | * ... 37 | * @author Iain Lobb - iainlobb@googlemail.com 38 | */ 39 | public class PlatformGamePadTester extends Sprite 40 | { 41 | private var character1:PlatformGameCharacter; 42 | private var character2:PlatformGameCharacter; 43 | private var gamePadView1:GamepadView; 44 | private var gamePadView2:GamepadView; 45 | 46 | public function PlatformGamePadTester() 47 | { 48 | addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 49 | } 50 | 51 | private function onAddedToStage(event:Event):void 52 | { 53 | removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 54 | 55 | createCharacters(); 56 | 57 | createGamepadViews(); 58 | 59 | addEventListener(Event.ENTER_FRAME, onEnterFrame); 60 | } 61 | 62 | private function createCharacters():void 63 | { 64 | character1 = new PlatformGameCharacter(0x669900, stage); 65 | character1.x = 100; 66 | character1.y = 200; 67 | character1.gamePad.useWASD(true); 68 | addChild(character1); 69 | 70 | character2 = new PlatformGameCharacter(0xFF6600, stage); 71 | character2.x = 300; 72 | character2.y = 200; 73 | addChild(character2); 74 | } 75 | 76 | private function createGamepadViews():void 77 | { 78 | gamePadView1 = new GamepadView(); 79 | gamePadView1.init(character1.gamePad); 80 | gamePadView1.x = 170; 81 | gamePadView1.y = 330; 82 | addChild(gamePadView1); 83 | 84 | gamePadView2 = new GamepadView(); 85 | gamePadView2.init(character2.gamePad, 0xFF6600); 86 | gamePadView2.x = 440; 87 | gamePadView2.y = 330; 88 | 89 | addChild(gamePadView2); 90 | } 91 | 92 | private function onEnterFrame(event:Event):void 93 | { 94 | character1.update(); 95 | character2.update(); 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepadtesters/PlatformGameCharacter.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepadtesters 27 | { 28 | import com.iainlobb.gamepad.Gamepad; 29 | import flash.display.MovieClip; 30 | import flash.display.Stage; 31 | /** 32 | * ... 33 | * @author Iain Lobb - iainlobb@googlemail.com 34 | */ 35 | public class PlatformGameCharacter extends MovieClip 36 | { 37 | public var gamePad:Gamepad; 38 | public var speedX:Number = 0; 39 | public var speedY:Number = 0; 40 | public var gravity:Number = 0.6; 41 | public var grounded:Boolean; 42 | public var maxSpeed:Number = 10; 43 | public var airAcceleration:Number = 0.5; 44 | private var ticksInAir:int; 45 | private var downwardGravity:Number = 1; 46 | 47 | public function PlatformGameCharacter(colour:int, stage:Stage) 48 | { 49 | graphics.beginFill(colour, 1); 50 | graphics.drawRect( -25, -25, 50, 50); 51 | graphics.endFill(); 52 | 53 | gamePad = new Gamepad(stage, false, 1); 54 | } 55 | 56 | public function update():void 57 | { 58 | if (grounded) 59 | { 60 | speedX += gamePad.x; 61 | } 62 | else 63 | { 64 | speedX += gamePad.x * airAcceleration; 65 | } 66 | 67 | speedX *= 0.9; 68 | 69 | if (speedX > maxSpeed) speedX = maxSpeed; 70 | if (speedX < -maxSpeed) speedX = -maxSpeed; 71 | 72 | x += speedX; 73 | y += speedY; 74 | 75 | if (y > 250) 76 | { 77 | y = 250; 78 | grounded = true; 79 | speedY = 0; 80 | } 81 | 82 | if (grounded) 83 | { 84 | ticksInAir = 0; 85 | } 86 | else 87 | { 88 | ticksInAir++; 89 | } 90 | 91 | // Alternate version for non-repeating jumps: 92 | //if ((grounded && gamePad.up.isPressed) || (!grounded && ticksInAir < 8 && gamePad.up.isDown)) 93 | 94 | if ((grounded || ticksInAir < 8) && gamePad.up.isDown) 95 | { 96 | speedY -= 2; 97 | grounded = false; 98 | } 99 | 100 | if (speedY > 0) 101 | { 102 | speedY += downwardGravity; 103 | } 104 | else 105 | { 106 | speedY += gravity; 107 | } 108 | } 109 | } 110 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepad/GamepadMultiInput.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb - iainlobb@gmail.com 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepad 27 | { 28 | public class GamepadMultiInput 29 | { 30 | protected var _isDown:Boolean; 31 | protected var _isPressed:Boolean; 32 | protected var _isReleased:Boolean; 33 | protected var _downTicks:int = -1; 34 | protected var _upTicks:int = -1; 35 | protected var isOr:Boolean; 36 | protected var inputs:Array; 37 | 38 | /* 39 | * Represents a virtual button that combines the input of 2 or more other buttons, e.g. up-left/north-west. End users shouldn't need to create these. 40 | * 41 | */ 42 | public function GamepadMultiInput(inputs:Array, isOr:Boolean) 43 | { 44 | this.inputs = inputs; 45 | this.isOr = isOr; 46 | } 47 | 48 | /* 49 | * Called by owner Gamepad. End users should not call this function. 50 | */ 51 | public function update():void 52 | { 53 | if (isOr) 54 | { 55 | _isDown = false; 56 | 57 | for each (var gamepadInput:GamepadInput in inputs) 58 | { 59 | if (gamepadInput.isDown) 60 | { 61 | _isDown = true; 62 | break; 63 | } 64 | } 65 | } 66 | else 67 | { 68 | _isDown = true; 69 | 70 | for each (var gamepadInput:GamepadInput in inputs) 71 | { 72 | if (!gamepadInput.isDown) 73 | { 74 | _isDown = false; 75 | break; 76 | } 77 | } 78 | } 79 | 80 | 81 | if (_isDown) 82 | { 83 | _isPressed = _downTicks == -1; 84 | _isReleased = false; 85 | _downTicks++; 86 | _upTicks = -1; 87 | } 88 | else 89 | { 90 | _isReleased = _upTicks == -1; 91 | _isPressed = false; 92 | _upTicks++; 93 | _downTicks = -1; 94 | } 95 | } 96 | 97 | /* 98 | * Is this input currently held down. 99 | */ 100 | public function get isDown():Boolean { return _isDown; } 101 | 102 | /* 103 | * Was this input pressed this frame/step - use instead of listening to key down events. 104 | */ 105 | public function get isPressed():Boolean { return _isPressed; } 106 | 107 | /* 108 | * Was this input released this frame/step - use instead of listening to key up events. 109 | */ 110 | public function get isReleased():Boolean { return _isReleased; } 111 | 112 | /* 113 | * How long has the input been held down. 114 | */ 115 | public function get downTicks():int { return _downTicks; } 116 | 117 | /* 118 | * How long since the input was last released. 119 | */ 120 | public function get upTicks():int { return _upTicks; } 121 | 122 | } 123 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepad/GamepadInput.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb - iainlobb@gmail.com 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepad 27 | { 28 | public class GamepadInput 29 | { 30 | protected var _isDown:Boolean; 31 | protected var _isPressed:Boolean; 32 | protected var _isReleased:Boolean; 33 | protected var _downTicks:int = -1; 34 | protected var _upTicks:int = -1; 35 | protected var mappedKeys:Array; 36 | 37 | /* 38 | * Represents a gamepad button - can be mapped to more than 1 physical key simultaneously, allowing multiple redundant control schemes. 39 | */ 40 | public function GamepadInput(keyCode:int = -1) 41 | { 42 | mappedKeys = (keyCode > -1) ? [keyCode] : []; 43 | } 44 | 45 | /* 46 | * Map a physical key to this virtual button. 47 | * @param keyCode Use the constants from com.cheezeworld.utils.KeyCode. 48 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 49 | */ 50 | public function mapKey(keyCode:int, replaceExisting:Boolean = false):void 51 | { 52 | if (replaceExisting) 53 | { 54 | mappedKeys = [keyCode]; 55 | } 56 | else if (mappedKeys.indexOf(keyCode) == -1) 57 | { 58 | mappedKeys.push(keyCode); 59 | } 60 | } 61 | 62 | /* 63 | * Unmap a physical key from this virtual button. 64 | * @param keyCode Use the constants from com.cheezeworld.utils.KeyCode. 65 | */ 66 | public function unmapKey(keyCode:int):void 67 | { 68 | mappedKeys.splice(mappedKeys.indexOf(keyCode), 1); 69 | } 70 | 71 | /* 72 | * Called by owner Gamepad. End users should not call this function. 73 | */ 74 | public function update():void 75 | { 76 | if (_isDown) 77 | { 78 | _isPressed = _downTicks == -1; 79 | _isReleased = false; 80 | _downTicks++; 81 | _upTicks = -1; 82 | } 83 | else 84 | { 85 | _isReleased = _upTicks == -1; 86 | _isPressed = false; 87 | _upTicks++; 88 | _downTicks = -1; 89 | } 90 | } 91 | 92 | /* 93 | * Called by owner Gamepad. End users should not call this function. 94 | */ 95 | public function keyDown(keyCode:int):void 96 | { 97 | if (mappedKeys.indexOf(keyCode) > -1) _isDown = true; 98 | } 99 | 100 | /* 101 | * Called by owner Gamepad. End users should not call this function. 102 | */ 103 | public function keyUp(keyCode:int):void 104 | { 105 | if (mappedKeys.indexOf(keyCode) > -1) _isDown = false; 106 | } 107 | 108 | /* 109 | * Is this input currently held down. 110 | */ 111 | public function get isDown():Boolean { return _isDown; } 112 | 113 | /* 114 | * Was this input pressed this frame/step - use instead of listening to key down events. 115 | */ 116 | public function get isPressed():Boolean { return _isPressed; } 117 | 118 | /* 119 | * Was this input released this frame/step - use instead of listening to key up events. 120 | */ 121 | public function get isReleased():Boolean { return _isReleased; } 122 | 123 | /* 124 | * How long has the input been held down. 125 | */ 126 | public function get downTicks():int { return _downTicks; } 127 | 128 | /* 129 | * How long since the input was last released. 130 | */ 131 | public function get upTicks():int { return _upTicks; } 132 | } 133 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepadtesters/GamePadTester.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepadtesters 27 | { 28 | import com.cheezeworld.utils.KeyCode; 29 | import com.iainlobb.gamepad.Gamepad; 30 | import com.iainlobb.gamepad.GamepadView; 31 | import flash.display.MovieClip; 32 | import flash.display.Sprite; 33 | import flash.events.Event; 34 | 35 | /** 36 | * ... 37 | * @author Iain Lobb - iainlobb@googlemail.com 38 | */ 39 | public class GamePadTester extends Sprite 40 | { 41 | private var gamePad1:Gamepad; 42 | private var gamePad2:Gamepad; 43 | private var character:MovieClip; 44 | private var car:MovieClip; 45 | private var gamePadView1:GamepadView; 46 | private var gamePadView2:GamepadView; 47 | 48 | public function GamePadTester() 49 | { 50 | addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 51 | } 52 | 53 | private function onAddedToStage(event:Event):void 54 | { 55 | removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 56 | 57 | createGamepads(); 58 | 59 | createGamepadViews(); 60 | 61 | createCharacter(); 62 | 63 | createCar(); 64 | 65 | addEventListener(Event.ENTER_FRAME, onEnterFrame); 66 | } 67 | 68 | private function createGamepads():void 69 | { 70 | gamePad1 = new Gamepad(stage, true); 71 | gamePad1.useWASD(true); 72 | gamePad1.useIJKL(false); 73 | 74 | gamePad2 = new Gamepad(stage, false, 0.4); 75 | } 76 | 77 | private function createGamepadViews():void 78 | { 79 | gamePadView1 = new GamepadView(); 80 | gamePadView1.init(gamePad1); 81 | gamePadView1.x = 170; 82 | gamePadView1.y = 330; 83 | addChild(gamePadView1); 84 | 85 | gamePadView2 = new GamepadView(); 86 | gamePadView2.init(gamePad2, 0xFF6600); 87 | gamePadView2.x = 440; 88 | gamePadView2.y = 330; 89 | addChild(gamePadView2); 90 | } 91 | 92 | private function createCharacter():void 93 | { 94 | character = new MovieClip(); 95 | character.graphics.beginFill(0x669900, 1); 96 | //character.graphics.drawRect( -25, -25, 50, 50); 97 | character.graphics.moveTo(0, 20); 98 | character.graphics.lineTo(20, 0); 99 | character.graphics.lineTo(-20, 0); 100 | character.graphics.lineTo(0, 20); 101 | character.graphics.drawCircle(0, 0, 25); 102 | character.graphics.endFill(); 103 | character.x = 100; 104 | character.y = 200; 105 | addChild(character); 106 | } 107 | 108 | private function createCar():void 109 | { 110 | car = new MovieClip(); 111 | car.graphics.beginFill(0xFF6600, 1); 112 | car.graphics.moveTo(0, -20); 113 | car.graphics.lineTo(20, 0); 114 | car.graphics.lineTo(-20, 0); 115 | car.graphics.lineTo(0, -20); 116 | car.graphics.drawRect(-25, -50, 50, 100); 117 | car.graphics.endFill(); 118 | car.x = 300; 119 | car.y = 200; 120 | addChild(car); 121 | } 122 | 123 | private function onEnterFrame(event:Event):void 124 | { 125 | // Character (the square) 126 | 127 | character.x += gamePad1.x * 5; 128 | character.y += gamePad1.y * 5; 129 | character.rotation = -gamePad1.rotation; 130 | 131 | // Car (the rectangle) 132 | 133 | // TANK CONTROLS: 134 | /* 135 | if (gamePad2.y <= 0) 136 | { 137 | car.rotation += gamePad2.x * 2; 138 | } 139 | else 140 | { 141 | car.rotation -= gamePad2.x * 2; 142 | } 143 | */ 144 | 145 | // CAR CONTROLS 146 | car.rotation += gamePad2.x * -gamePad2.y * 2; 147 | 148 | var carAngle:Number = -car.rotation * (Math.PI / 180); 149 | 150 | car.x += Math.sin(carAngle) * gamePad2.y * 5; 151 | car.y += Math.cos(carAngle) * gamePad2.y * 5; 152 | } 153 | 154 | } 155 | 156 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepad/GamepadView.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb - iainlobb@gmail.com 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepad 27 | { 28 | import flash.display.Sprite; 29 | import flash.events.Event; 30 | 31 | public class GamepadView extends Sprite 32 | { 33 | protected var ball:Sprite; 34 | protected var button1:Sprite; 35 | protected var button2:Sprite; 36 | protected var up:Sprite; 37 | protected var down:Sprite; 38 | protected var left:Sprite; 39 | protected var right:Sprite; 40 | protected var gamepad:Gamepad; 41 | protected var colour:uint; 42 | 43 | /* 44 | * Visual representation of a Gamepad instance. 45 | */ 46 | public function GamepadView() 47 | { 48 | 49 | } 50 | 51 | /* 52 | * Initialise the instance. 53 | * @param gamepad The Gamepad instance to show. 54 | * @param colour Hex value of the desired colour. 55 | */ 56 | public function init(gamepad:Gamepad, colour:uint = 0x669900):void 57 | { 58 | this.gamepad = gamepad; 59 | 60 | this.colour = colour; 61 | 62 | addEventListener(Event.ENTER_FRAME, onEnterFrame); 63 | 64 | drawBackground(); 65 | 66 | createBall(); 67 | 68 | createButtons(); 69 | 70 | createKeypad(); 71 | } 72 | 73 | protected function onEnterFrame(event:Event):void 74 | { 75 | ball.x = gamepad.x * 25; 76 | ball.y = gamepad.y * 25; 77 | 78 | button1.alpha = gamepad.fire1.isDown ? 1 : 0.2; 79 | button2.alpha = gamepad.fire2.isDown ? 1 : 0.2; 80 | 81 | up.alpha = gamepad.up.isDown ? 1 : 0.2; 82 | down.alpha = gamepad.down.isDown ? 1 : 0.2; 83 | left.alpha = gamepad.left.isDown ? 1 : 0.2; 84 | right.alpha = gamepad.right.isDown ? 1 : 0.2; 85 | } 86 | 87 | protected function drawBackground():void 88 | { 89 | if (gamepad.isCircle) 90 | { 91 | drawCircle(); 92 | } 93 | else 94 | { 95 | drawSquare(); 96 | } 97 | } 98 | 99 | protected function drawSquare():void 100 | { 101 | graphics.beginFill(colour, 0.2); 102 | graphics.drawRoundRect(-50, -50, 100, 100, 50, 50); 103 | graphics.endFill(); 104 | } 105 | 106 | protected function drawCircle():void 107 | { 108 | graphics.beginFill(colour, 0.2); 109 | graphics.drawCircle(0, 0, 50); 110 | graphics.endFill(); 111 | } 112 | 113 | protected function createBall():void 114 | { 115 | ball = new Sprite(); 116 | ball.graphics.beginFill(colour, 1); 117 | ball.graphics.drawCircle(0, 0, 25); 118 | ball.graphics.endFill(); 119 | addChild(ball); 120 | } 121 | 122 | protected function createKeypad():void 123 | { 124 | up = createKey(); 125 | up.x = -125; 126 | up.y = -15; 127 | 128 | down = createKey(); 129 | down.x = -125; 130 | down.y = 20; 131 | 132 | left = createKey(); 133 | left.x = -160; 134 | left.y = 20; 135 | 136 | right = createKey(); 137 | right.x = -90; 138 | right.y = 20; 139 | } 140 | 141 | protected function createButtons():void 142 | { 143 | button1 = createButton(); 144 | button1.x = 75; 145 | 146 | button2 = createButton(); 147 | button2.x = 75; 148 | button2.y = 35; 149 | } 150 | 151 | protected function createButton():Sprite 152 | { 153 | var button:Sprite = new Sprite(); 154 | button.graphics.beginFill(colour, 1); 155 | button.graphics.drawCircle(0, 0, 15); 156 | button.graphics.endFill(); 157 | addChild(button); 158 | 159 | return button; 160 | } 161 | 162 | protected function createKey():Sprite 163 | { 164 | var key:Sprite = new Sprite(); 165 | key.graphics.beginFill(colour, 1); 166 | key.graphics.drawRoundRect(0, 0, 30, 30, 20, 20); 167 | key.graphics.endFill(); 168 | addChild(key); 169 | 170 | return key; 171 | } 172 | 173 | } 174 | 175 | } -------------------------------------------------------------------------------- /com/cheezeworld/utils/KeyCode.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2008 Cheezeworld.com 3 | 4 | Additional key constants added by Iain Lobb. 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the "Software"), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | 29 | package com.cheezeworld.utils 30 | { 31 | public class KeyCode 32 | { 33 | //LETTERS 34 | public static const A:int = 65; 35 | public static const B:int = 66; 36 | public static const C:int = 67; 37 | public static const D:int = 68; 38 | public static const E:int = 69; 39 | public static const F:int = 70; 40 | public static const G:int = 71; 41 | public static const H:int = 72; 42 | public static const I:int = 73; 43 | public static const J:int = 74; 44 | public static const K:int = 75; 45 | public static const L:int = 76; 46 | public static const M:int = 77; 47 | public static const N:int = 78; 48 | public static const O:int = 79; 49 | public static const P:int = 80; 50 | public static const Q:int = 81; 51 | public static const R:int = 82; 52 | public static const S:int = 83; 53 | public static const T:int = 84; 54 | public static const U:int = 85; 55 | public static const V:int = 86; 56 | public static const W:int = 87; 57 | public static const X:int = 88; 58 | public static const Y:int = 89; 59 | public static const Z:int = 90; 60 | 61 | //NUMBERS 62 | public static const ZERO:int = 48; 63 | public static const ONE:int = 49; 64 | public static const TWO:int = 50; 65 | public static const THREE:int = 51; 66 | public static const FOUR:int = 52; 67 | public static const FIVE:int = 53; 68 | public static const SIX:int = 54; 69 | public static const SEVEN:int = 55; 70 | public static const EIGHT:int = 56; 71 | public static const NINE:int = 57; 72 | 73 | //NUMPAD 74 | public static const NUMPAD_0:int = 96; 75 | public static const NUMPAD_1:int = 97; 76 | public static const NUMPAD_2:int = 98; 77 | public static const NUMPAD_3:int = 99; 78 | public static const NUMPAD_4:int = 100; 79 | public static const NUMPAD_5:int = 101; 80 | public static const NUMPAD_6:int = 102; 81 | public static const NUMPAD_7:int = 103; 82 | public static const NUMPAD_8:int = 104; 83 | public static const NUMPAD_9:int = 105; 84 | public static const NUMPAD_MULTIPLY:int = 106; 85 | public static const NUMPAD_ADD:int = 107; 86 | public static const NUMPAD_ENTER:int = 108; 87 | public static const NUMPAD_SUBTRACT:int = 109; 88 | public static const NUMPAD_DECIMAL:int = 110; 89 | public static const NUMPAD_DIVIDE:int = 111; 90 | 91 | //FUNCTION KEYS 92 | public static const F1:int = 112; 93 | public static const F2:int = 113; 94 | public static const F3:int = 114; 95 | public static const F4:int = 115; 96 | public static const F5:int = 116; 97 | public static const F6:int = 117; 98 | public static const F7:int = 118; 99 | public static const F8:int = 119; 100 | public static const F9:int = 120; 101 | public static const F10:int = 121; 102 | public static const F11:int = 122; 103 | public static const F12:int = 123; 104 | public static const F13:int = 124; 105 | public static const F14:int = 125; 106 | public static const F15:int = 126; 107 | 108 | //SYMBOLS 109 | public static const COLON:int = 186; 110 | public static const EQUALS:int = 187; 111 | public static const UNDERSCORE:int = 189; 112 | public static const QUESTION_MARK:int = 191; 113 | public static const TILDE:int = 192; 114 | public static const OPEN_BRACKET:int = 219; 115 | public static const BACKWARD_SLASH:int = 220; 116 | public static const CLOSED_BRACKET:int = 221; 117 | public static const QUOTES:int = 222; 118 | public static const LESS_THAN:int = 188; 119 | public static const GREATER_THAN:int = 190; 120 | 121 | //OTHER KEYS 122 | public static const BACKSPACE:int = 8; 123 | public static const TAB:int = 9; 124 | public static const CLEAR:int = 12; 125 | public static const ENTER:int = 13; 126 | public static const SHIFT:int = 16; 127 | public static const CONTROL:int = 17; 128 | public static const ALT:int = 18; 129 | public static const CAPS_LOCK:int = 20; 130 | public static const ESC:int = 27; 131 | public static const SPACEBAR:int = 32; 132 | public static const PAGE_UP:int = 33; 133 | public static const PAGE_DOWN:int = 34; 134 | public static const END:int = 35; 135 | public static const HOME:int = 36; 136 | public static const LEFT:int = 37; 137 | public static const UP:int = 38; 138 | public static const RIGHT:int = 39; 139 | public static const DOWN:int = 40; 140 | public static const INSERT:int = 45; 141 | public static const DELETE:int = 46; 142 | public static const HELP:int = 47; 143 | public static const NUM_LOCK:int = 144; 144 | 145 | 146 | 147 | 148 | } 149 | } -------------------------------------------------------------------------------- /com/iainlobb/gamepad/Gamepad.as: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010 Iain Lobb - iainlobb@gmail.com 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | package com.iainlobb.gamepad 27 | { 28 | import com.cheezeworld.utils.KeyCode; 29 | import flash.events.Event; 30 | import flash.events.KeyboardEvent; 31 | import flash.ui.Keyboard; 32 | import flash.display.Stage; 33 | 34 | public class Gamepad 35 | { 36 | // INPUTS: 37 | 38 | protected var _up:GamepadInput; 39 | protected var _down:GamepadInput; 40 | protected var _left:GamepadInput; 41 | protected var _right:GamepadInput; 42 | protected var _fire1:GamepadInput; 43 | protected var _fire2:GamepadInput; 44 | protected var _inputs:Array; 45 | 46 | // MULTI-INPUTS (combines 2 or more inputs into 1, e.g. _upLeft requires both up and left to be pressed) 47 | 48 | protected var _upLeft:GamepadMultiInput; 49 | protected var _downLeft:GamepadMultiInput; 50 | protected var _upRight:GamepadMultiInput; 51 | protected var _downRight:GamepadMultiInput; 52 | protected var _anyDirection:GamepadMultiInput; 53 | protected var _multiInputs:Array; 54 | 55 | // THE "STICK" 56 | 57 | protected var _x:Number = 0; 58 | protected var _y:Number = 0; 59 | protected var _targetX:Number = 0; 60 | protected var _targetY:Number = 0; 61 | protected var _angle:Number = 0; 62 | protected var _rotation:Number = 0; 63 | protected var _magnitude:Number = 0; 64 | 65 | /* 66 | * Prevents diagonal movement being faster than horizontal or vertical movement. Use for top-down view games. 67 | */ 68 | public var isCircle:Boolean; 69 | 70 | /* 71 | * Simple ease-out speed (range 0 to 1). Pass value of 1 to prevent easing. 72 | */ 73 | public var ease:Number; 74 | 75 | /* 76 | * Gamepad simplifies keyboard input by simulating an analog joystick. 77 | * @param stage A reference to the stage is needed to listen for system events 78 | * @param isCircle Prevents diagonal movement being faster than horizontal or vertical movement. Use for top-down view games. 79 | * @param ease Simple ease-out speed (range 0 to 1). Pass value of 1 to prevent easing. 80 | * @param autoStep Pass in false if you intend to call step() manually. 81 | */ 82 | 83 | private var stage:Stage; 84 | 85 | public function Gamepad(stage:Stage, isCircle:Boolean, ease:Number = 0.2, autoStep:Boolean = true) 86 | { 87 | trace('Remember to hit "Control > Disable Keyboard Shorcuts" in the Flash IDE & stand-alone Flash player!'); 88 | 89 | this.isCircle = isCircle; 90 | this.ease = ease; 91 | this.stage = stage; 92 | 93 | _up = new GamepadInput(); 94 | _down = new GamepadInput(); 95 | _left = new GamepadInput(); 96 | _right = new GamepadInput(); 97 | _fire1 = new GamepadInput(); 98 | _fire2 = new GamepadInput(); 99 | 100 | _inputs = [_up, _down, _left, _right, _fire1, _fire2]; 101 | 102 | _upLeft = new GamepadMultiInput([_up, _left], false); 103 | _upRight = new GamepadMultiInput([_up, _right], false); 104 | _downLeft = new GamepadMultiInput([_down, _left], false); 105 | _downRight = new GamepadMultiInput([_down, _right], false); 106 | _anyDirection = new GamepadMultiInput([_up, _down, _left, _right], true); 107 | 108 | _multiInputs = [_upLeft, _upRight, _downLeft, _downRight, _anyDirection]; 109 | 110 | useArrows(); 111 | useControlSpace(); 112 | 113 | stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true); 114 | stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 0, true); 115 | 116 | if (autoStep) 117 | { 118 | stage.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true); 119 | } 120 | } 121 | 122 | /* 123 | * Destructor. 124 | */ 125 | 126 | public function destroy():void { 127 | stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 128 | stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp); 129 | stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame); 130 | } 131 | 132 | // DIRECTION PRESETS 133 | 134 | /* 135 | * Quickly map all direction keys 136 | * @param up Keycode - use the constants from com.cheezeworld.utils.KeyCode. 137 | * @param down Keycode - use the constants from com.cheezeworld.utils.KeyCode. 138 | * @param left Keycode - use the constants from com.cheezeworld.utils.KeyCode. 139 | * @param right Keycode - use the constants from com.cheezeworld.utils.KeyCode. 140 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 141 | */ 142 | 143 | public function mapDirection(up:int, down:int, left:int, right:int, replaceExisting:Boolean = false):void 144 | { 145 | _up.mapKey(up, replaceExisting); 146 | _down.mapKey(down, replaceExisting); 147 | _left.mapKey(left, replaceExisting); 148 | _right.mapKey(right, replaceExisting); 149 | } 150 | 151 | /* 152 | * Preset to use the direction arrow keys for movement 153 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 154 | */ 155 | 156 | public function useArrows(replaceExisting:Boolean = false):void 157 | { 158 | mapDirection(Keyboard.UP, Keyboard.DOWN, Keyboard.LEFT, Keyboard.RIGHT, replaceExisting); 159 | } 160 | 161 | /* 162 | * Preset to use the W, A, S and D keys for movement. This layout doesn't work for players with French AZERTY keyboards - call useZQSD() instead. 163 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 164 | */ 165 | 166 | public function useWASD(replaceExisting:Boolean = false):void 167 | { 168 | mapDirection(KeyCode.W, KeyCode.S, KeyCode.A, KeyCode.D, replaceExisting); 169 | } 170 | 171 | /* 172 | * Preset to use the I, J, K and L keys for movement. 173 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 174 | */ 175 | 176 | public function useIJKL(replaceExisting:Boolean = false):void 177 | { 178 | mapDirection(KeyCode.I, KeyCode.K, KeyCode.J, KeyCode.L, replaceExisting); 179 | } 180 | 181 | /* 182 | * Preset to use the Z, Q, S and D keys for movement. Use this mapping instead of useWASD() when targeting French AZERTY keyboards. 183 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 184 | */ 185 | 186 | public function useZQSD(replaceExisting:Boolean = false):void 187 | { 188 | mapDirection(KeyCode.Z, KeyCode.S, KeyCode.Q, KeyCode.D, replaceExisting); 189 | } 190 | 191 | // FIRE BUTTON PRESETS 192 | 193 | /* 194 | * Map the fire buttons. 195 | * @param fire1 The primary fire button. 196 | * @param fire2 The secondary fire button. 197 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 198 | */ 199 | 200 | public function mapFireButtons(fire1:int, fire2:int, replaceExisting:Boolean = false):void 201 | { 202 | _fire1.mapKey(fire1, replaceExisting); 203 | _fire2.mapKey(fire2, replaceExisting); 204 | } 205 | 206 | /* 207 | * Preset to use the < and > keys for fire buttons. 208 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 209 | */ 210 | 211 | public function useChevrons(replaceExisting:Boolean = false):void 212 | { 213 | mapFireButtons(KeyCode.LESS_THAN, KeyCode.GREATER_THAN, replaceExisting); 214 | } 215 | 216 | /* 217 | * Preset to use the G and H keys for fire buttons. 218 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 219 | */ 220 | 221 | public function useGH(replaceExisting:Boolean = false):void 222 | { 223 | mapFireButtons(KeyCode.G, KeyCode.H, replaceExisting); 224 | } 225 | 226 | /* 227 | * Preset to use the Z and X keys for fire buttons. 228 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 229 | */ 230 | 231 | public function useZX(replaceExisting:Boolean = false):void 232 | { 233 | mapFireButtons(KeyCode.Z, KeyCode.X, replaceExisting); 234 | } 235 | 236 | /* 237 | * Preset to use the Y and X keys for fire buttons. 238 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 239 | */ 240 | 241 | public function useYX(replaceExisting:Boolean = false):void 242 | { 243 | mapFireButtons(KeyCode.Y, KeyCode.X, replaceExisting); 244 | } 245 | 246 | /* 247 | * Preset to use the CTRL and SPACEBAR keys for fire buttons. 248 | * @param replaceExisting pass true to replace exisiting keys, false to add mapping without replacing existing keys. 249 | */ 250 | 251 | public function useControlSpace(replaceExisting:Boolean = false):void 252 | { 253 | mapFireButtons(KeyCode.CONTROL, KeyCode.SPACEBAR, replaceExisting); 254 | } 255 | 256 | // UPDATE: 257 | 258 | /* 259 | * Step/Update the gamepad. Called automatically if you didn't pass in autoStep as false. This should be called in sync with you game/physics step. 260 | */ 261 | 262 | public function step():void 263 | { 264 | _x += (_targetX - _x) * ease; 265 | _y += (_targetY - _y) * ease; 266 | 267 | _magnitude = Math.sqrt((_x * _x) + (_y * _y)); 268 | 269 | _angle = Math.atan2(_x, _y); 270 | 271 | _rotation = _angle * 57.29577951308232; 272 | 273 | for each (var gamepadInput:GamepadInput in _inputs) gamepadInput.update(); 274 | } 275 | 276 | // GETTERS: 277 | 278 | /* 279 | * The angle of the direction pad in radians. 280 | */ 281 | public function get angle():Number { return _angle; } 282 | 283 | /* 284 | * The horizontal component of the direction pad, value between 0 and 1. 285 | */ 286 | public function get x():Number { return _x; } 287 | 288 | /* 289 | * The vertical component of the direction pad, value between 0 and 1. 290 | */ 291 | public function get y():Number { return _y; } 292 | 293 | /* 294 | * A GamepadInput representing the up/north direction. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 295 | */ 296 | public function get up():GamepadInput { return _up; } 297 | 298 | /* 299 | * A GamepadInput representing the down/south direction. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 300 | */ 301 | public function get down():GamepadInput { return _down; } 302 | 303 | /* 304 | * A GamepadInput representing the left/west direction. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 305 | */ 306 | public function get left():GamepadInput { return _left; } 307 | 308 | /* 309 | * A GamepadInput representing the right/east direction. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 310 | */ 311 | public function get right():GamepadInput { return _right; } 312 | 313 | /* 314 | * A GamepadMultiInput representing the up-left/north-west direction. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. End-users should not need to configure this input. 315 | */ 316 | public function get upLeft():GamepadMultiInput { return _upLeft; } 317 | 318 | /* 319 | * A GamepadMultiInput representing the down-left/south-west direction. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. End-users should not need to configure this input. 320 | */ 321 | public function get downLeft():GamepadMultiInput { return _downLeft; } 322 | 323 | /* 324 | * A GamepadMultiInput representing the up-right/north-east direction. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. End-users should not need to configure this input. 325 | */ 326 | public function get upRight():GamepadMultiInput { return _upRight; } 327 | 328 | /* 329 | * A GamepadMultiInput representing the down-right/south-east direction. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. End-users should not need to configure this input. 330 | */ 331 | public function get downRight():GamepadMultiInput { return _downRight; } 332 | 333 | /* 334 | * A GamepadInput representing the primary fire button. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 335 | */ 336 | public function get fire1():GamepadInput { return _fire1; } 337 | 338 | /* 339 | * A GamepadInput representing the secondary fire button. Configure with its mapKey and unmapKey functions. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. 340 | */ 341 | public function get fire2():GamepadInput { return _fire2; } 342 | 343 | /* 344 | * A special GamepadMultiInput representing whether any direction is pressed. Get state information from isDown, isPressed, isReleased, downTicks and upTicks. End-users should not need to configure this input. 345 | */ 346 | public function get anyDirection():GamepadMultiInput { return _anyDirection; } 347 | 348 | /* 349 | * The length/magnitude of the direction pad, between 0 and 1. 350 | */ 351 | public function get magnitude():Number { return _magnitude; } 352 | 353 | /* 354 | * the angle of the direction pad in degrees, between 0 and 360. 355 | */ 356 | public function get rotation():Number { return _rotation; } 357 | 358 | // PROTECTED METHODS: 359 | 360 | protected function onEnterFrame(event:Event):void 361 | { 362 | step(); 363 | } 364 | 365 | protected function onKeyDown(event:KeyboardEvent):void 366 | { 367 | for each (var gamepadInput:GamepadInput in _inputs) gamepadInput.keyDown(event.keyCode); 368 | 369 | updateState(); 370 | } 371 | 372 | protected function onKeyUp(event:KeyboardEvent):void 373 | { 374 | for each (var gamepadInput:GamepadInput in _inputs) gamepadInput.keyUp(event.keyCode); 375 | 376 | updateState(); 377 | } 378 | 379 | protected function updateState():void 380 | { 381 | for each (var gamepadMultiInput:GamepadMultiInput in _multiInputs) gamepadMultiInput.update(); 382 | 383 | if (_up.isDown) 384 | { 385 | _targetY = -1; 386 | } 387 | else if (_down.isDown) 388 | { 389 | _targetY = 1; 390 | } 391 | else 392 | { 393 | _targetY = 0; 394 | } 395 | 396 | if (_left.isDown) 397 | { 398 | _targetX = -1; 399 | } 400 | else if (_right.isDown) 401 | { 402 | _targetX = 1; 403 | } 404 | else 405 | { 406 | _targetX = 0; 407 | } 408 | 409 | var _targetAngle:Number = Math.atan2(_targetX, _targetY); 410 | 411 | if (isCircle && _anyDirection.isDown) 412 | { 413 | _targetX = Math.sin(_targetAngle); 414 | _targetY = Math.cos(_targetAngle); 415 | } 416 | } 417 | } 418 | } --------------------------------------------------------------------------------