Skip to content

Commit

Permalink
fix: failing assertion in flame library
Browse files Browse the repository at this point in the history
  'package:flame/src/components/sprite_animation_group_component.dart': Failed assertion: line 143 pos 12: '_animations != null': Animations not set
  dart:core                                                                  _AssertionError._throwNew
  package:flame/src/components/sprite_animation_group_component.dart 143:12  SpriteAnimationGroupComponent.current=
  package:ricochlime/flame/components/monster.dart 372:7                     MonsterAnimation.walking=
  • Loading branch information
adil192 committed Sep 7, 2024
1 parent 886a740 commit 2f7dd24
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions lib/flame/components/monster.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ class MonsterAnimation extends SpriteAnimationGroupComponent<MonsterState>
removeOnFinish: {
MonsterState.dead: true,
},
);
) {
animations = getAnimations(monsterImagePath: monsterImagePath);
}

static const staticWidth = 20.0;
static const staticHeight = staticWidth;
Expand All @@ -377,7 +379,6 @@ class MonsterAnimation extends SpriteAnimationGroupComponent<MonsterState>

@override
Future<void> onLoad() async {
animations = getAnimations();
current = MonsterState.idle;
walking = _walking;
await super.onLoad();
Expand Down Expand Up @@ -408,7 +409,9 @@ class MonsterAnimation extends SpriteAnimationGroupComponent<MonsterState>
final animations = this.animations;
if (animations == null) return;

final monsterImage = gameRef.images.fromCache(monsterImagePath);
// Swap the sprite in existing animations
final monsterImage =
RicochlimeGame.instance.images.fromCache(monsterImagePath);
for (final animation in animations.values) {
for (final frame in animation.frames) {
frame.sprite.image = monsterImage;
Expand All @@ -417,8 +420,11 @@ class MonsterAnimation extends SpriteAnimationGroupComponent<MonsterState>
}

/// The list of animations for the monster.
Map<MonsterState, SpriteAnimation> getAnimations() {
final monsterImage = gameRef.images.fromCache(monsterImagePath);
static Map<MonsterState, SpriteAnimation> getAnimations({
required String monsterImagePath,
}) {
final monsterImage =
RicochlimeGame.instance.images.fromCache(monsterImagePath);
return {
MonsterState.idle: SpriteAnimation.fromFrameData(
monsterImage,
Expand Down
4 changes: 3 additions & 1 deletion test/new_row_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ void main() {
group('New row generation', () {
test(
'At least ${RicochlimeGame.minMonstersInRow} and '
'at most ${Monster.monstersPerRow} monsters are generated', () {
'at most ${Monster.monstersPerRow} monsters are generated', () async {
TestWidgetsFlutterBinding.ensureInitialized();
SharedPreferences.setMockInitialValues({});
Prefs.testingMode = true;
Prefs.init();
await RicochlimeGame.instance.preloadSprites.future;

final random = Random(12);
expect(RicochlimeGame.minMonstersInRow, lessThan(Monster.monstersPerRow));
Expand Down

0 comments on commit 2f7dd24

Please sign in to comment.