grinmonk

Input events propagation

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.