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

View File

@ -11,6 +11,9 @@ class SpriteAnimationComponent extends PositionComponent {
Paint overridePaint; Paint overridePaint;
bool removeOnFinish = false; 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( SpriteAnimationComponent(
Vector2 size, Vector2 size,
this.animation, { this.animation, {
@ -19,62 +22,33 @@ class SpriteAnimationComponent extends PositionComponent {
super.size.setFrom(size); super.size.setFrom(size);
} }
/// Creates a component with an empty animation which can be set later
SpriteAnimationComponent.empty(); 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, Vector2 size,
Image image, Image image,
int amount, { SpriteAnimationData data, {
int amountPerRow,
Vector2 texturePosition,
@required double stepTime,
Vector2 textureSize,
bool loop = true,
this.removeOnFinish = false, this.removeOnFinish = false,
}) { }) {
super.size.setFrom(size); super.size.setFrom(size);
animation = SpriteAnimation.sequenced( animation = SpriteAnimation.fromFrameData(
image, image,
amount, data,
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,
); );
} }
@override @override
bool get shouldRemove => removeOnFinish && animation.isLastFrame; bool get shouldRemove => removeOnFinish && (animation?.isLastFrame ?? false);
@mustCallSuper @mustCallSuper
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
super.render(canvas); super.render(canvas);
animation.getSprite().render( animation?.getSprite()?.render(
canvas, canvas,
size: size, size: size,
overridePaint: overridePaint, overridePaint: overridePaint,
@ -84,6 +58,6 @@ class SpriteAnimationComponent extends PositionComponent {
@override @override
void update(double t) { void update(double t) {
super.update(t); super.update(t);
animation.update(t); animation?.update(t);
} }
} }