fix: Unable to run the demo game created with very_good create flame game
Ricardo-MT opened this issue · comments
Description
Existing errors in the unicorn.dart file prevent me from running the game:
"The setter 'onComplete' isn't defined for the type 'SpriteAnimation'.
Try importing the library that defines 'onComplete', correcting the name to the name of an existing setter, or defining a setter or field named 'onComplete'."
"The setter 'currentIndex' isn't defined for the type 'SpriteAnimation'.
Try importing the library that defines 'currentIndex', correcting the name to the name of an existing setter, or defining a setter or field named 'currentIndex'"
"The method 'update' isn't defined for the type 'SpriteAnimation'.
Try correcting the name to the name of an existing method, or defining a method named 'update'."
"The method 'reset' isn't defined for the type 'SpriteAnimation'.
Try correcting the name to the name of an existing method, or defining a method named 'reset'."
"The getter 'isFirstFrame' isn't defined for the type 'SpriteAnimation'.
Try importing the library that defines 'isFirstFrame', correcting the name to the name of an existing getter, or defining a getter or field named 'isFirstFrame'."
Steps To Reproduce
- Create a project with very_good create flame_game
- Attempt to run the game (it'll fail)
- Go to lib/game/entities/unicorn/unicorn.dart
- See errors
Expected Behavior
I was expecting to create the project and be able to run it out of the box .
Additional Context
Apparently all this properties and method references causing the errors were move to the class SpriteAnimationTicker, hence I was able to solve this issue by adding a SpriteAnimationTicker instance as a class property.
This is the result:
import 'package:flame/components.dart';
import 'package:flame/sprite.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flutter/material.dart';
import 'package:prototype/game/entities/unicorn/behaviors/tapping_behavior.dart';
import 'package:prototype/gen/assets.gen.dart';
class Unicorn extends PositionedEntity with HasGameRef {
Unicorn({
required super.position,
}) : super(
anchor: Anchor.center,
size: Vector2.all(32),
behaviors: [
TappingBehavior(),
],
);
@visibleForTesting
Unicorn.test({
required super.position,
super.behaviors,
}) : super(size: Vector2.all(32));
SpriteAnimation? _animation;
late SpriteAnimationTicker _animationTicker;
@visibleForTesting
SpriteAnimation get animation => _animation!;
@visibleForTesting
SpriteAnimationTicker get animationTicker => _animationTicker;
@override
Future<void> onLoad() async {
_animation = await gameRef.loadSpriteAnimation(
Assets.images.unicornAnimation.path,
SpriteAnimationData.sequenced(
amount: 16,
stepTime: 0.1,
textureSize: Vector2.all(32),
loop: false,
),
);
final animationComponent =
SpriteAnimationComponent(animation: _animation, size: size);
_animationTicker = animationComponent.animationTicker!;
resetAnimation();
_animationTicker.onComplete = resetAnimation;
await add(animationComponent);
}
/// Set the animation to the first frame by tricking the animation
/// into thinking it finished the last frame.
void resetAnimation() {
_animationTicker
..currentIndex = _animation!.frames.length - 1
..update(0.1)
..currentIndex = 0;
}
/// Plays the animation.
void playAnimation() => _animationTicker.reset();
/// Returns whether the animation is playing or not.
bool isAnimationPlaying() => !_animationTicker.isFirstFrame;
}
EDITED: Happy to create a PR.
Hi @Ricardo-MT, thanks for opening this issue! I was able to reproduce this and it should get fixed as soon as possible. Are you still interested in creating a Pull Request?
Hi @alestiago . Yes absolutely
Thank you @Ricardo-MT , looking forward to it!
You might want to start by reading the CONTRIBUTING or have a look at the template repository which uses mason for the code generation. Let us know if you have any questions!
Hey @Ricardo-MT,
As this is a priority for the next VG CLI release, I tackled this in this morning When tracking another card (VeryGoodOpenSource/very_good_flame_game#103) for this same issue.
Tracking fix on VeryGoodOpenSource/very_good_flame_game#109