Jump to content

Get receiver of click event


DerDree
 Share

Recommended Posts

I just started working with phaser and so far its really brilliant. But now I am at a point where I could need some help and I hope I will find it here  :)

 

Is there a way to get the receiver of a click event? I know that you can enable inputs on sprites and add a handler for a click event. But I would like to have a general click handler which checks which element has received the click.

 

As a background why I need this: I have multiple objects, each can be selected and with a click in the level moved to the clicked position. So I want to check if a object received the click and then select it or if for example a tile received the click and then move the current selected object.

Link to comment
Share on other sites

I do the following: for all objects created I provide a unique name. Then I attach a callback function that will use the name to differentiate which one was clicked.

 

A sample:

// creating components and attaching callback functionthis.cfg = this.add.sprite(0, 0, 'cfg');this.cfg.name = 'cfg'this.cfg.inputEnabled = true;this.cfg.events.onInputUp.add(this.onControlClicked, this);this.sound = this.add.sprite(0, 0, 'sound');this.sound.name = 'sound'this.sound.inputEnabled = true;this.sound.events.onInputUp.add(this.onControlClicked, this);
// callback functiononControlClicked: function(target){    var t = target;    if ('cfg' === t.name) {            // here I handle events when cfg sprite was clicked            return;    }    if ('sound' === t.name) {            // here I handle events when cfg sprite was clicked            return;    }}

I hope it helps you.

Link to comment
Share on other sites

Thank you. Yes I think this is what I am looking for.

 

Another way to implement what I want would be to check if certain tiles were clicked so that I can see if it was a move command on a floor tile. Is there a way to link click handlers to certain tile types?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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