Sign in to follow this  
Brad P. Taylor

limiting child sprite input

Recommended Posts

Hi All,

 

While working on the ui for my project I needed to disable input for sprites that were clipped to a smaller scrolling view (think scrolling list), I peeked around the Input and InputHandler code and didn't see an way to restrict the input for the child from a parent (or grandparent)?  Anyway I cobbled together a way to clip input for decedents. The code can be found in a small gist here

 

http://goo.gl/LTfFOy

 

It's pretty simple it modifies the prototype for InputHandler, by adding an ancestor search after any truthy result returned by the original checkPointerDown or CheckPointerOver. This allows any ancestor to define a property (defaults to 'input_clipping_rect') which will can be used to reject false positives (ie things outside the smaller view but inside the child). 

 

Here is the code too just in case the gist isn't available. 

 

(function(property) {

 

    var local = new Phaser.Point();

    function askAncestors( child, pointer ) {

        for ( var parent = child.parent; parent; parent = parent.parent ) {

            var clip = parent[property];

            if ( !clip ) continue;

            parent.game.input.getLocalPosition( parent, pointer, local);

            var cx = local.x - clip.x, cy = local.y - clip.y;

            return (0 <= cx) && (cx < clip.width) && (0 <= cy) && (cy < clip.height);

        }

        return true;

    }

 

    var _checkPointerDown_ = Phaser.InputHandler.prototype.checkPointerDown;

    Phaser.InputHandler.prototype.checkPointerDown = function(pointer, fastTest) {

        if ( !_checkPointerDown_.call( this, pointer, fastTest ) ) return false;

        return askAncestors( this.sprite, pointer );

    }

 

    var _checkPointerOver_ = Phaser.InputHandler.prototype.checkPointerOver;

    Phaser.InputHandler.prototype.checkPointerOver = function(pointer, fastTest) {

        if ( !_checkPointerOver_.call( this, pointer, fastTest ) ) return false;

        return askAncestors( this.sprite, pointer );

    }

 

})( 'input_clipping_rect' );

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.