Jump to content

How to get total width of sprite with its children


bymafmaf
 Share

Recommended Posts

I have a parentSprite that is created with no key. Such as:

var parentSprite = game.add.sprite(0,0);

Its only mission is to hold some child sprites. But when I try to get

parentSprite.width

it only gives me parentSprite's width which is 0. Is there a built-in method for that purpose or some workaround?

Link to comment
Share on other sites

That would add every sprite's width, which is not what I want because some sprites are under another ones. So I need its bounds that create a rectangle.

 

There is a function called getLocalBounds which returns a rectangle but it doesn't show the true width, I don't know why.

Link to comment
Share on other sites

That would add every sprite's width, which is not what I want because some sprites are under another ones. So I need its bounds that create a rectangle.

 

There is a function called getLocalBounds which returns a rectangle but it doesn't show the true width, I don't know why.

Can you elaborate more on what you have and what you are actually trying to get?

 

You have a sprite that has children and those children have other children?

Link to comment
Share on other sites

var minLeft = game.world.width;var maxRight = 0;parentSprite.children.forEach(function(child){  if(child.left < minLeft) minLeft=child.left;  if(child.right > maxRight) maxRight=child.right;});var parentWidth=maxRight-minLeft;

Something like this might work? Though this may cause a performance issue if you have a lot of child sprites.

 

The same logic can be applied for the height.

Link to comment
Share on other sites

Something like this would give the bounding width of a sprite's immediate children but no deeper than that, it includes visibility checking thus will exclude hidden children in the calculation

 

    PIXI.DisplayObjectContainer.prototype.getBounds.call(parentSprite).width

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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