kriket Posted October 8, 2015 Share Posted October 8, 2015 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 More sharing options...
drhayes Posted October 8, 2015 Share Posted October 8, 2015 You could set a custom property on it, like "kriketBounds" with a Phaser.Rectangle in it. Then check against that. Link to comment Share on other sites More sharing options...
Skeptron Posted October 8, 2015 Share Posted October 8, 2015 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 More sharing options...
DonFrag Posted October 8, 2015 Share Posted October 8, 2015 i was thinking in add a second invisible sprite for overlaping check.create the overlaping sprite, addChild it and use it for checking but i think that the rectangle solution could be more efficient. Link to comment Share on other sites More sharing options...
kriket Posted October 8, 2015 Author Share Posted October 8, 2015 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 More sharing options...
Skeptron Posted October 9, 2015 Share Posted October 9, 2015 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 More sharing options...
darkraziel Posted October 13, 2016 Share Posted October 13, 2016 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 More sharing options...
Recommended Posts