Sign in to follow this  
pencilking2002

increase sprite hit area without increasing its body

Recommended Posts

I would like to increase a sprite's hit area so that its easier to tap on it on mobile. I would like to keep its body the same size however as there are collisions in the game that depend on it. How can I accomplish this? 

 

I tried 

paddle.body.setSize(500, 200, 0, 0);

 

but this seems to increase the body size, screwing up collisions.

 

Thanks! 

Share this post


Link to post
Share on other sites

There is a hitArea property on all pixi DisplayObjects, which Phaser's objects inherit from. You can feed this a new Phaser.Rectangle like so:

// add a hypothetical 10x10 sprite
var sprite = game.add.sprite(0, 0, 'test');

// make the hit area 20x20 - note you can also use negative figures for the first two params (x and y) to offset it from the sprite
sprite.hitArea = new Phaser.Rectangle(0, 0, 20, 20);

I don't know for sure if this works with all input methods but I've tested it with dragging and it works fine there.

Share this post


Link to post
Share on other sites

Hi, lewster32 says good tip.
But you should remember about sprite anchor

So, I use such a universal snippet.  Positive HIT_AREA_PADDING increase hit area, negative - shrink it

var HIT_AREA_PADDING = 16;

sprite.hitArea = new Phaser.Rectangle( 
        sprite.left-HIT_AREA_PADDING, 
        sprite.top-HIT_AREA_PADDING,
        sprite.width + HIT_AREA_PADDING * 2,
        sprite.height + HIT_AREA_PADDING * 2
    );

 

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.