Fixing animation example

This commit is contained in:
Erick Zanardo
2020-12-05 19:06:42 -03:00
committed by renancaraujo
parent 5124ee6a79
commit 4f7c1065e5
2 changed files with 33 additions and 55 deletions

View File

@ -30,26 +30,30 @@ class MyGame extends BaseGame with TapDetector {
chopper = await images.load('chopper.png');
creature = await images.load('creature.png');
animation = SpriteAnimation.sequenced(
animation = SpriteAnimation.fromFrameData(
chopper,
4,
SpriteAnimationData.sequenced(
amount: 4,
textureSize: Vector2.all(48),
stepTime: 0.15,
loop: true,
),
);
}
void addAnimation(double x, double y) {
final size = Vector2(291, 178);
final animationComponent = SpriteAnimationComponent.sequenced(
final animationComponent = SpriteAnimationComponent.fromFrameData(
size,
creature,
18,
SpriteAnimationData.sequenced(
amount: 18,
amountPerRow: 10,
textureSize: size,
stepTime: 0.15,
loop: false,
),
removeOnFinish: true,
);
@ -78,6 +82,6 @@ class MyGame extends BaseGame with TapDetector {
}
MyGame(Vector2 screenSize) {
size = screenSize;
size.setFrom(screenSize);
}
}

View File

@ -11,6 +11,9 @@ class SpriteAnimationComponent extends PositionComponent {
Paint overridePaint;
bool removeOnFinish = false;
/// Creates an [SpriteAnimationComponent] from an [animation] and a [size]
///
/// Optionally [removeOnFinish] can be set to true to have this component be auto removed from the [BaseGame] when the animation is finished.
SpriteAnimationComponent(
Vector2 size,
this.animation, {
@ -19,62 +22,33 @@ class SpriteAnimationComponent extends PositionComponent {
super.size.setFrom(size);
}
/// Creates a component with an empty animation which can be set later
SpriteAnimationComponent.empty();
SpriteAnimationComponent.sequenced(
/// Creates a SpriteAnimationComponent from a [size], an [image] and [data], check [SpriteAnimationData] for more info on the available options.
///
/// Optionally [removeOnFinish] can be set to true to have this component be auto removed from the [BaseGame] when the animation is finished.
SpriteAnimationComponent.fromFrameData(
Vector2 size,
Image image,
int amount, {
int amountPerRow,
Vector2 texturePosition,
@required double stepTime,
Vector2 textureSize,
bool loop = true,
SpriteAnimationData data, {
this.removeOnFinish = false,
}) {
super.size.setFrom(size);
animation = SpriteAnimation.sequenced(
animation = SpriteAnimation.fromFrameData(
image,
amount,
amountPerRow: amountPerRow,
texturePosition: texturePosition,
textureSize: textureSize,
stepTime: stepTime ?? 0.1,
loop: loop,
);
}
SpriteAnimationComponent.variableSequenced(
Vector2 size,
Image image,
int amount,
List<double> stepTimes, {
int amountPerRow,
Vector2 texturePosition,
Vector2 textureSize,
bool loop = true,
}) {
super.size.setFrom(size);
animation = SpriteAnimation.variableSequenced(
image,
amount,
stepTimes,
amountPerRow: amountPerRow,
texturePosition: texturePosition,
textureSize: textureSize,
loop: loop,
data,
);
}
@override
bool get shouldRemove => removeOnFinish && animation.isLastFrame;
bool get shouldRemove => removeOnFinish && (animation?.isLastFrame ?? false);
@mustCallSuper
@override
void render(Canvas canvas) {
super.render(canvas);
animation.getSprite().render(
animation?.getSprite()?.render(
canvas,
size: size,
overridePaint: overridePaint,
@ -84,6 +58,6 @@ class SpriteAnimationComponent extends PositionComponent {
@override
void update(double t) {
super.update(t);
animation.update(t);
animation?.update(t);
}
}