Jump to content

Find sprite around


titmael
 Share

Recommended Posts

Hi, titmael.

 

Have you look at the Phaser examples? Specifically, have you looked at the 'Overlap Without Physics' example?  You can create a new Phaser.Rectangle object and use its functions to check for overlap and if a certain set of coordinates are within that rectangle.

 

Worst case, you can do the math yourself, checking if some coordinates are within a calculated circle too.

Link to comment
Share on other sites

Clever idea ! But that means I have to change my sprite body shape (and I don't want to :P ) ?

 

Not necessarily. You can add something like a 'triggerBox' property that is a Phaser.Rectangle and use that instead.

var spriteA = this.game.add.sprite(0,0, cacheKey);spriteA.triggerBox = new Phaser.Rectangle(x, y, width, height);var spriteB = this.game.add.sprite(0,0, cacheKey);

And then, to check, something like the following:

if(spriteA.triggerBox.intersects(spriteB.getBounds())) {  // spriteB overlaps spriteA.triggerBox's rectangle}
Link to comment
Share on other sites

The Phaser.Math.distance method could be used for finding objects within a radius. In basic use you'd just loop through all of the sprites you want to check and do a comparison between their positions and the center point to get how far away they are from it. It's a pretty fast method so it should work fine even with lots of sprites, but if you're keen on optimisation then you'll maybe want to look into 'area of interest' techniques, QuadTrees and so on.

var centerPoint = new Phaser.Point(50, 50);var triggerDistance = 100;spriteGroup.forEachAlive(function(sprite) {  if (Phaser.Math.distance(sprite.x, sprite.y, centerPoint.x, centerPoint.y) <= triggerDistance) {    // do something to this sprite, as it lies within the trigger distance  }});
Link to comment
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...
 Share

  • Recently Browsing   0 members

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