Jump to content

how to add multiple keys


Rootar
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...