Jump to content

changing sprite bounds (no physics enabled, no body). Possible?


kriket
 Share

Recommended Posts

Is it possible to change a sprite's bounds. I am checking if it overlaps another sprite manually and without physics (http://phaser.io/examples/v2/sprites/overlap-without-physics) and dont need physics in the game at all. But the sprite is like a tree branch for example. Big trunk which I want the player to "overlap" with but not the leaves. If the branch had physics, it was as easy as 

 

        branch.body.setSize(850, 10, 100, 75);    
 
 
But how can I change the bounding box size for a sprite?
Link to comment
Share on other sites

A quick solution that comes to my mind would be to use Rectangles and Phaser.Rectangle.intersects(rect1, rect2).

 

I think you can use sprite.getBounds(), which should work even without physics, and which returns a Rectangle with the size and coordinates of the sprite. You could shrink / expand it before doing the intersects() check.

Link to comment
Share on other sites

Theres so many sprites in the scene that i have to do this for, i am now wondering whether it will give me any benefit at all for not using the physics engine. Have about 10 such sprites that move (without physics) and will need a ghost rectangle. Will see and post results. 

Link to comment
Share on other sites

If it's 10 times the same sprite (say, "tree"), then you can create a Tree class with a child sprite for overlap (like DonFrag suggested), or use the Rectangle strategy. This way, no matter what number of trees there are in the scene, the code will keep short and simple.

 

Or you could create a super, abstract parent Sprite class which comes with a child / Rect. And have all your sprite inherit from it, no matter what they are.

Link to comment
Share on other sites

  • 1 year later...

I had to do exactly this for a project I worked some time ago, and ended up going with the Rectangles approach. My game objects were classes that extended Phaser.Sprite and each of them had a Phaser.Rectangle that updated it's position on update.

this.rectangle.x = this.sprite.x;
this.rectangle.y = this.sprite.y;

And then when I needed to check for collision I just used Phaser.Rectangle.intersects(), even with a lot of sprites on screen (around 45-50) it worked like a charm.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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