Jump to content

How to update bounds for nested sprites


Recommended Posts

Hi there. I have a noob question... How I can update bounds of nested sprites?


For example:

I have a sprite

levelSprite = this.game.add.sprite(0, 0); //now width and height - 32x32

This sprite has two childs


levelNum1 = this.game.add.sprite(0, 0, 'game_assets_4');levelNum1.frameName = "gm_level_number_0001.png";levelNum2 = this.game.add.sprite(100, 100, 'game_assets_4');levelNum2.frameName = "gm_level_number_0002.png";levelSprite.addChild(levelNum1);levelSprite.addChild(levelNum2); // now width and height still 32x32... why?


I use phaser v. 2.4.3

Would you be so kind to help me please...

Link to comment
Share on other sites

unfortunately it doesn't work like that...


here's an example that will calculate what you need



this is what @rich said.


bounds is, width/height are effectively the scale of the object, the texture size if you will
even a sprite with no assigned texture (like the forum post) still has a texture ... hence, it has dimensions
it’s more how it’s called.. a PIXI Sprite getBounds doesn’t iterate children, it’s just the display bounds of the Sprite itself - whereas a DisplayObjectContainer (like a Group) iterates just children
Pixi overrides the DoC getBounds with the Sprite one, because the result is used for the uv verts
to get a complete bounds of a Sprite with children you’d need to do a getBounds on the Sprite and sum that rect with a getBounds on its children

also remember if you’re doing bounds calls within a create function it hasn’t had a chance for the render pass yet, so you’d need to call updateTransform on it first 




Link to comment
Share on other sites


  • Recently Browsing   0 members

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