darkcreator Posted March 28, 2018 Share Posted March 28, 2018 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 More sharing options...
snowbillr Posted March 29, 2018 Share Posted March 29, 2018 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 More sharing options...
darkcreator Posted March 29, 2018 Author Share Posted March 29, 2018 Thanks @snowbillr I'm need use another way to implement my logic. The Phaser2 way is the best for me, but I will do something differente =/. I will use the string event ('keydown_A') anyway, its works but I like much more add listerner direct on keys. Link to comment Share on other sites More sharing options...
PsichiX Posted March 29, 2018 Share Posted March 29, 2018 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 More sharing options...
mcofko Posted August 16, 2018 Share Posted August 16, 2018 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? Link to comment Share on other sites More sharing options...
freeeve Posted August 22, 2018 Share Posted August 22, 2018 @mcofko I'm running into the same issue, using some tutorial code, but simplified. (I'm also a newbie, but just wanted to chime in, in case you figured it out or to follow this post.) Link to comment Share on other sites More sharing options...
samme Posted August 22, 2018 Share Posted August 22, 2018 Works for me. https://codepen.io/samme/pen/XYJaQE freeeve 1 Link to comment Share on other sites More sharing options...
mcofko Posted August 22, 2018 Share Posted August 22, 2018 Tried your link and it works flawlessly. I've noticed that key events work normal if I run the game from VS (with IIS Express), but it does not work if I start the game directly in Chrome with running simple web server (python). Super weird. freeeve 1 Link to comment Share on other sites More sharing options...
freeeve Posted September 2, 2018 Share Posted September 2, 2018 I figured it out. It was the evernote web clipper chrome plugin messing with keyboard input. I realized it might be a chrome issue when I tested in firefox and it worked fine. (someone else posted a similar issue here)... a121514191, Joecool and Jonas Luz Jr. 1 2 Link to comment Share on other sites More sharing options...
Recommended Posts