Created
August 10, 2011 23:07
-
-
Save grapefrukt/1138529 to your computer and use it in GitHub Desktop.
Using closures for fun and profit
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
public function InputManager(players:GameObjectCollection, keyCodes:Vector.<int>){ | |
_players = players; | |
_inputs = new Vector.<OneButtonInput>(Settings.NUM_PLAYERS, Settings.NUM_PLAYERS); | |
for (var i:int = 0; i < Settings.NUM_PLAYERS; i++) { | |
_inputs[i] = new OneButtonInput(_players.collection[i].stage, keyCodes[i]); | |
var makeClosure:Function = function(arg:int):Function { | |
return function(e:Event):void{ handleChange(e, arg)}; | |
} | |
_inputs[i].addEventListener(Event.CHANGE, makeClosure(i)); | |
} | |
} | |
private function handleChange(e:Event, index:int):void { | |
Player(_players.collection[index]).setInputState(OneButtonInput(e.target).isDown); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment