Skip to content

Instantly share code, notes, and snippets.

@grapefrukt
Created August 10, 2011 23:07
Show Gist options
  • Save grapefrukt/1138529 to your computer and use it in GitHub Desktop.
Save grapefrukt/1138529 to your computer and use it in GitHub Desktop.
Using closures for fun and profit
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