bymafmaf Posted December 12, 2015 Share Posted December 12, 2015 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 getparentSprite.widthit 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 More sharing options...
Batzi Posted December 12, 2015 Share Posted December 12, 2015 Try something like thisvar totalWidth = 0;parentSprite.children.forEach(function(child){ totalWidth += child.width;}) Link to comment Share on other sites More sharing options...
bymafmaf Posted December 12, 2015 Author Share Posted December 12, 2015 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 More sharing options...
Batzi Posted December 13, 2015 Share Posted December 13, 2015 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 More sharing options...
Gyutang Posted December 13, 2015 Share Posted December 13, 2015 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 More sharing options...
bymafmaf Posted December 13, 2015 Author Share Posted December 13, 2015 That workaround should work but I think there should be feature for that. Because it's particularly important for touch events to know the rectangle that encapsulates it all. Link to comment Share on other sites More sharing options...
stupot Posted December 13, 2015 Share Posted December 13, 2015 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 More sharing options...
drhayes Posted December 14, 2015 Share Posted December 14, 2015 Phaser's got a library function that'll do this: Phaser.Rectangle.aabb. Link to comment Share on other sites More sharing options...
Recommended Posts