Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Problems with Keyboard events.


darkcreator
 Share

Recommended Posts

Hi everyone, are fine ?

Sorry if this question is too simple but I'm losing my sanity for some hours and I don't reach solve the problem alone. 

I migrating a old project made in Phaser2 to Phaser3 and maybe I the lost the way to do things in Phaser3 way. I trying to the something bellow(phaser2) in Phaser3:

    input.keyboard.addKey(Phaser.Input.Keyboard.LEFT).onDown(callback);

But it don't work on Phaser3, Key don't have onDown method (don't have any method that I see in source code) and I don't know a way to add eventListener to keys. I really don't espect to use verifications on update function, has another way to process keyboard input events ?

Thanks for any help.

 

Link to comment
Share on other sites

If you want to add a listener for a specific key, you don't do it on that key object anymore. Instead, you listen on the keyboard object for a specific event.

this.input.keyboard.on('keydown_A', function (event) {
  console.log('Hello from the A Key!');
});

 

If you do care about a specific key, then you can still add that key and look at it's `isDown` property in the update function:

...

create: function() {
  this.key = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
}

update: function() {
  if (this.key.isDown) {
    console.log('A is pressed');
  }
}

...

This examples shows both methods - https://labs.phaser.io/edit.html?src=src\input\keyboard\keydown.js

Link to comment
Share on other sites

if you really want to keep this v2 like way, you can make a wrapper over phaser keyboard event and register callbacks:

class KeyEvent {
  constructor(keyboard, name) {
    this._keyboard = keyboard;
    this._name = name;
  }

  destroy() {
    this._keyboard = null;
    this._name = null;
  }

  on(callback) {
    this._keyboard.on(this._name, callback);
  }

  off(callback) {
    this._keyboard.off(this._name, callback);
  }

  once(callback) {
    this._keyboard.once(this._name, callback);
  }
}

// on Stage init:

const keyDown = new KeyEvent(this.input.keyboard, 'keydown_A');
keyDown.once(() => console.log('A key pressed (called once)'));
keyDown.on(() => console.log('A key pressed'));

But man, this may be an overkill - you should definitely use phaser input manager directly.

Link to comment
Share on other sites

  • 4 months later...

Hi everyone,

i'll reuse this topic, because I have similar issue. I'm complete beginner with Phaser, and I've just started with this simple game tutorial. I've stumbled on a problem with key events.  

So, I've just copied the code as it is written in tutorial: 
 

function create ()
{
  cursors = this.input.keyboard.createCursorKeys();
}

function update ()
{
    if (cursors.left.isDown) {
        player.setVelocityX(-160);
        player.anims.play('left', true);
        console.log("Going Left!");
    }
}

Super simple code. Once I press Left Arrow key, isDown flag stays on TRUE. Check the attached script. And i'm using the latest version of phaser.min.js.

Did anyone stumble upon same issue?

image.png.0c1396e738a37e5937a8aa323e66e493.png

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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