flixel
flixel copied to clipboard
FlxSpriteGroup's get_width() doesn't take into account child FlxSpriteGroup's, can return incorrect number
I posted this issue in Discord as well, please see the video: https://discord.com/channels/162395145352904705/165234904815239168/843562833894965278
Try out the code from the video to see the issue yourself:
class PlayState extends FlxState
{
var parentGroup:FlxSpriteGroup;
var widthText:FlxText;
function getChildGroup()
{
var group = new FlxSpriteGroup();
var thing = new FlxSprite(-10, -10);
thing.makeGraphic(10, 10, FlxColor.RED, true);
group.add(thing);
var thing2 = new FlxSprite(0, 0);
thing2.makeGraphic(10, 10, FlxColor.BLUE, true);
group.add(thing2);
return group;
}
override public function create()
{
parentGroup = new FlxSpriteGroup();
parentGroup.setPosition(FlxG.width / 2, FlxG.height / 2);
add(parentGroup);
parentGroup.add(getChildGroup());
widthText = new FlxText(0, 0, 0, Std.string(parentGroup.width));
add(widthText);
}
override public function update(elapsed:Float)
{
if (FlxG.keys.justPressed.SPACE)
{
var sprite = new FlxSprite(-10, 10);
sprite.makeGraphic(10, 10, FlxColor.GREEN, true);
parentGroup.add(sprite);
widthText.text = Std.string(parentGroup.width);
}
super.update(elapsed);
}
}
From my interpretation of what a sprite's width is, width is the difference between the sprite's leftmost pixel's x to its rightmost pixel's x.
FlxSpriteGroup's get_width calculations seem to assume this as well, but only if all the sprites inside the group are just FlxSprites and not groups. https://github.com/HaxeFlixel/flixel/blob/master/flixel/group/FlxSpriteGroup.hx#L813
It doesn't take into account that in a group, the leftmost pixel may not be at group.x. It could have a child sprite that is at negative coordinates relative to itself. Meaning group.x + group.width does not return where the right most pixel is. Meaning that get_width does not always accurately answer the question: "In this group, what is the distance between the group's leftmost pixel and rightmost pixel?"
And as you can see in the video, the parentGroup's width is being calculated as 30, even though I added a sprite (the green square) which should have no affect on parentGroup's width.
This seems like a bug to me. Thoughts/opinions?
I'm too lazy to recreate this test in a sterile environment, what values are you seeing before and after space is pressed, and what values are you expecting? just by looking, I'm expecting the width, before and after to be 20. is that not the case?
sorry for finally, getting back to this. I tried out your example and yes this is a bug. Fix incoming