Skip to content

Commit

Permalink
Fix FlxSpriteGroup origin (#2981)
Browse files Browse the repository at this point in the history
* update children origins relative to their positions

* add originTransform unit test
  • Loading branch information
47rooks authored Dec 15, 2023
1 parent 3594fe0 commit 58bfba6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
Sprite.offset.copyFrom(Offset);

inline function originTransform(Sprite:FlxSprite, Origin:FlxPoint)
Sprite.origin.copyFrom(Origin);
Sprite.origin.set(x + origin.x - Sprite.x, y + origin.y - Sprite.y);

inline function scaleTransform(Sprite:FlxSprite, Scale:FlxPoint)
Sprite.scale.copyFrom(Scale);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/src/flixel/group/FlxSpriteGroupTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ class FlxSpriteGroupTest extends FlxTest
Assert.isFalse(member2.revived);
return group;
}
@Test
/**
* Ensure that member origins are correctly set when
* the group origin is set.
*/
function testOriginTransform()
{
var f1 = new FlxSprite(-10, 100);
var f2 = new FlxSprite(50, 50);
group.add(f1);
group.add(f2);

group.setPosition(280, 300);
group.origin.set(300, 400);

// Verify positions are updated - absolute
Assert.areEqual(270, f1.x);
Assert.areEqual(400, f1.y);
Assert.areEqual(330, f2.x);
Assert.areEqual(350, f2.y);

// Verify origins are correct - relative
Assert.areEqual(310, f1.origin.x);
Assert.areEqual(300, f1.origin.y);
Assert.areEqual(250, f2.origin.x);
Assert.areEqual(350, f2.origin.y);
}
}

class Member extends FlxSprite
Expand Down

0 comments on commit 58bfba6

Please sign in to comment.