Last active
August 29, 2015 14:27
-
-
Save Hasufel/e2fbf8135227735f5bad to your computer and use it in GitHub Desktop.
Gamepad handling with multiple ships moving example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import openfl.display.Sprite; | |
import openfl.events.Event; | |
import openfl.Lib; | |
import lime.ui.Gamepad; | |
import lime.ui.GamepadAxis; | |
import lime.ui.GamepadButton; | |
class Main extends Sprite { | |
private var _ships:Map<Int,Particle>; | |
private var _gamepads:Array<Int> = []; | |
private var _speedRange:Int = 5; | |
public function new() { | |
super(); | |
init(); | |
} | |
private function init():Void { | |
this.mouseEnabled = false; | |
this.mouseChildren = false; | |
_ships = new Map<Int,Particle>(); | |
Lib.application.window.onGamepadButtonDown.add (onButtonDown); | |
Lib.application.window.onGamepadButtonUp.add (onButtonUp); | |
Lib.application.window.onGamepadAxisMove.add (onAxisMove); | |
Lib.application.window.onGamepadConnect.add(onGamepadConnect); | |
Lib.application.window.onGamepadDisconnect.add(onGamepadDisconnect); | |
addEventListener(Event.ENTER_FRAME, onEnter); | |
} | |
private function onButtonDown (g:Gamepad, b:GamepadButton):Void { | |
if (!searchGamepad(g.id)) return; | |
trace(b + ' pressed' + ' on gamepad ' + g.id); | |
} | |
private function onButtonUp (g:Gamepad, b:GamepadButton):Void { | |
if (!searchGamepad(g.id)) return; | |
trace(b + ' released' + ' on gamepad ' + g.id); | |
} | |
private function onAxisMove (g:Gamepad, a:GamepadAxis, v:Float):Void { | |
if ((v > 0 && v < 0.02) || (v < 0 && v > -0.02)) v = 0; //additional analog jitter filter | |
switch (Std.string(a)) { | |
case "LEFT_X": | |
trace('LEFT_X:' + v); | |
moveShip(g.id,0,v*_speedRange); | |
case "LEFT_Y": | |
trace('LEFT_Y:' + v); | |
moveShip(g.id,1,v*_speedRange); | |
case "RIGHT_X": trace('RIGHT_X:' + v); | |
case "RIGHT_Y": trace('RIGHT_Y:' + v); | |
case "TRIGGER_LEFT": trace('TRIGGER_LEFT:' + v); | |
case "TRIGGER_RIGHT": trace('TRIGGER_RIGHT:' + v); | |
default: trace(Std.string(a) + ':' + v); | |
} | |
} | |
private function onGamepadConnect (g:Gamepad):Void { | |
if (searchGamepad(g.id)) return; | |
else { | |
trace('gamepad ' + g.id + ' ('+ g.name + ') connected'); | |
_gamepads.push(g.id); | |
addShip(g.id); | |
} | |
} | |
private function onGamepadDisconnect (g:Gamepad):Void { | |
trace('gamepad ' + g.id + ' disconnected'); | |
_gamepads.remove(g.id); | |
removeShip(g.id); | |
} | |
private function searchGamepad (id:Int):Bool { | |
if (_gamepads.indexOf(id) == -1) return false; | |
return true; | |
} | |
private function addShip(n:Int):Void { | |
var ship:Particle = new Particle(randomInt(10,stage.stageWidth-10),randomInt(10,stage.stageHeight-10),0,0); | |
_ships.set(n,ship); | |
addChild(ship); | |
} | |
private function removeShip(n:Int):Void { | |
var ship:Particle = _ships.get(n); | |
_ships.remove(n); | |
removeChild(ship); | |
} | |
private function moveShip(n:Int,axis:Int,v:Float):Void { | |
var ship:Particle = _ships.get(n); | |
if (axis == 0) ship._vx = v; | |
else if (axis == 1) ship._vy = v; | |
} | |
private function onEnter(e:Event):Void { | |
for (ship in _ships) { | |
ship.x += ship._vx; | |
ship.y += ship._vy; | |
if (ship._vx < .1 && ship._vx > -.1) ship._vx = 0; | |
if (ship._vy < .1 && ship._vy > -.1) ship._vy = 0; | |
if (ship.x < 0) ship.x = 0; | |
else if (ship.x+ship.width > stage.stageWidth) ship.x = stage.stageWidth-ship.width; | |
if (ship.y < 0) ship.y = 0; | |
else if (ship.y+ship.height > stage.stageHeight) ship.y = stage.stageHeight-ship.height; | |
} | |
} | |
private function randomInt(low:Int, high:Int):Int{ | |
return Math.round(Math.random() * (high - low) + low); | |
} | |
} | |
class Particle extends Sprite { | |
public var _vx:Float; | |
public var _vy:Float; | |
public var _c:Int; | |
public function new(x:Int, y:Int, vx:Float = 0, vy:Float = 0) { | |
super(); | |
_vx = vx; | |
_vy = vy; | |
_c = Std.int(Math.random()*0xFFFFFF); | |
this.graphics.beginFill(_c); | |
this.graphics.drawRect(0,0,10,10); | |
this.graphics.endFill(); | |
this.x = x; | |
this.y = y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment