Skip to content

Commit

Permalink
fix: Allow setting bounds while BoundedPositionBehavior's target …
Browse files Browse the repository at this point in the history
…is null (#2926)

The `bounds` setter of `BoundedPositionBehavior` tries to update the
target's position when bounds are updated. But it wasn't checking if the
target is null. This was causing null exceptions while updating bounds
of an unmounted `BoundedPositionBehavior` with null target (as seen in
[this failing test
case](https://github.com/flame-engine/flame/actions/runs/7231182930/job/19704091006#step:5:1291)).
This PR fixes that by checking if the target is null before updating the
position.


Closes #2655
  • Loading branch information
ufrshubham authored Dec 17, 2023
1 parent f6d4b6c commit bab9be6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class BoundedPositionBehavior extends Component {
_bounds = newBounds;
if (!isValidPoint(_previousPosition)) {
_previousPosition.setFrom(_bounds.center);
update(0);
if (_target != null) {
update(0);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ void main() {
);
});

test('update bounds while target is null', () {
final circle1 = Circle(Vector2.zero(), 10);
final circle2 = Circle(Vector2.all(30), 10);
final boundedPositionBehavior = BoundedPositionBehavior(bounds: circle1);
expect(() => boundedPositionBehavior.bounds = circle2, returnsNormally);
});

testWithFlameGame('bad parent', (game) async {
final shape = Circle(Vector2.zero(), 10);
final parent = Component()..addToParent(game);
Expand Down

0 comments on commit bab9be6

Please sign in to comment.