Jump to content

Best way to handle event


Recommended Posts

Hey there guys! 

I working on chess game with Phaser 3. And so far everything is amazing. Right now I'm working on event handlers for the pieces(figures). Take a look:


 for(let i = 0; i < this.pieces.length; i++) {
    this.pieces[i].on('clicked', ()=>{ 
                     console.log('Piece clicked'));
                     }, this);

This is taken from the example HERE

So my problem is this fragrament of the code:

  this.input.on('gameobjectup', function (pointer, gameObject)
              gameObject.emit('clicked', gameObject);
            }, this);

Can someone explain the whole structure? Or maybe suggest cleaner way to do it?

Link to comment
Share on other sites

The structure looks fine.  It is a listener you are assigning for when the input event handler gets and event of "gameobjectup" to execute a callback anonymous function you have set up.

You could destructure your callback into a named function instead if you'd like to destructure the event itself from the callback.  Here is an example of working code for your specific example that makes a named function that can be referenced later, see destroyBox function below:


const destroyBox = gameObject => gameObject.emit('clicked', gameObject);
this.input.on('gameobjectdown', (pointer, gameObject)=> destroyBox(gameObject));


I understand javascript (especially ES6 stuff) quite extensively.  Where I have difficulties is interacting with the new phaser 3 api as the documentation is pretty sparse (the examples are great dont get me wrong, but there is no real handy API reference I've found yet).

Link to comment
Share on other sites


  • Recently Browsing   0 members

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