Skip to content

Commit

Permalink
fix: Size for SpriteComponent.fromImage should be nullable (#3054)
Browse files Browse the repository at this point in the history
I received an assertion error after I set `autoResize` to true when I am
using `SpriteComponent.fromImage` component. It's kinda weird for me
that the message tells me **"If size is set, autoResize should be false
or size should be null when autoResize is true.''**, but the thing is I
never set the size for that image yet. Then, I check the code and I
found out that the size will never be null anyway, so the `autoresize`
should always be false.

In this PR, I just remove all the fallback value for `size`, so it would
not give an assertion error when `size` is not being set yet while the
`autoresize` is true.

---------

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
  • Loading branch information
fa-fifi and spydon committed Mar 1, 2024
1 parent 741d938 commit 2ed71a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/flame/lib/src/components/sprite_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:flame/src/extensions/image.dart';
import 'package:meta/meta.dart';

export '../sprite.dart';
Expand Down Expand Up @@ -75,7 +74,7 @@ class SpriteComponent extends PositionComponent
autoResize: autoResize,
paint: paint,
position: position,
size: size ?? srcSize ?? image.size,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
Expand Down
12 changes: 12 additions & 0 deletions packages/flame/test/components/sprite_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ Future<void> main() async {
() => SpriteComponent(autoResize: true, size: Vector2.all(2)),
throwsAssertionError,
);
expect(
() => SpriteComponent.fromImage(
image,
autoResize: true,
size: Vector2.all(2),
),
throwsAssertionError,
);

expect(() => SpriteComponent(autoResize: false), throwsAssertionError);
expect(
() => SpriteComponent.fromImage(image, autoResize: false),
throwsAssertionError,
);
});

test('default value set correctly when not provided explicitly', () {
Expand Down

0 comments on commit 2ed71a3

Please sign in to comment.