Rootar Posted August 11, 2017 Share Posted August 11, 2017 Hello, I'm just starting my adventure with phaser. I have a question. Is it possible to add a few buttons to the variable so that you can later use the function addOnce? var upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP); upKey.onDown.addOnce(this.start, this); var wKey = game.input.keyboard.addKey(Phaser.Keyboard.W); wKey.onDown.addOnce(this.start, this); As you can see, this code does not look too nice. Is it possible to get rid of the redundancy? Link to comment Share on other sites More sharing options...
ldd Posted August 11, 2017 Share Posted August 11, 2017 const addOnce = (keyString, self) => { const key = game.input.keyboard.addKey(Phaser.Keyboard[keyString]); key.onDown.addOnce(self.start, self); } or maybe even const addKeys = (keyStrings, self) => { keyStrings.forEach( (keyString) => { const key = game.input.keyboard.addKey(Phaser.Keyboard[keyString]); key.onDown.addOnce(self.start, self); }); } //usage addKeys(['W','A','S','D'], something); Link to comment Share on other sites More sharing options...
Recommended Posts