Search code examples
javascriptphaser-framework2d-games

Phaser Game Keyboard .addKey Enter or Mobile Touch


How to add both options like Enter or Mobile touch to start

this.enter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER); 

Or

this.enter = this.input.on('pointerdown', this);

Solution

  • Depending on what you want the result to be, you could:

    create one function, that handles both events:

    function doAction(){
       alert('action')
    }
    

    and then reference the same function in the event listeners, of both actions:

    this.enter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER); 
    this.enter.on('down', doAction);
    
    // ... AND
    
    this.input.on('pointerdown', doAction);
    

    Update: If you want to, check what device type the app is being run, you could use the information of this.sys.game.device.os (link to documenation )