Jump to content

Input events propagation


grinmonk
 Share

Recommended Posts

I have a very trivial nested-sprites situation, similar to this:

function create() {

    var sprite = game.add.sprite(0, 0, 'phaser');
    sprite.width = 100;
    sprite.width = 100;
    
    sprite.inputEnabled = true;
    sprite.events.onInputDown.add(function(){console.log("parent clicked")});
    
    var child = game.make.sprite(0, 0, 'phaser');
    child.width = 10;
    child.width = 10;
    sprite.addChild(child);
    
    child.inputEnabled = true;
    child.events.onInputDown.add(function(){console.log("child clicked")});
    
    //child.input.priorityID = 1;
}

http://phaser.io/sandbox/edit/pfAcVgtC

The problem is that by default the parent catches the click event and does not propagate it to the child. How do I make both child and parent catch the click event?

Link to comment
Share on other sites

I don't see how. onInputDown I always want the ListView down event to fire, because if you then drag it the ListView should scroll. onInputUp events should fire to both the ListView (to stop the drag event) and the display object in the ListView (to execute whatever function is assigned to it).

There are ways to solve this but because ListView is an open source library, the api needs to be simple. It makes sense to me that you can add Display Objects with input event listeners to a ListView and both the ListView events and the ListView's children's events will both fire. 

priorityID let's you choose which event to capture first, but doesn't let you capture multiple events.

Currently my workaround is to capture the ListView's events, and then onInputUp I search for children under the pointer and fire their onInputUp events. It works, but it's ugly in my code.

Link to comment
Share on other sites

Rich has answered on slack that it's not possible to propagate click events in Phaser.

I definitely feel it would be a good feature. Hopefully Lazer will account for this.

I've written a quick function to find the next child under a point with inputEnabled set to true to get around this.

Link to comment
Share on other sites

  • 8 months later...
 Share

  • Recently Browsing   0 members

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