Sign in to follow this  
Rootar

how to add multiple keys

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?

Share this post


Link to post
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);

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.