mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
Adding destoryOnFinish flag to AnimationComponent
This commit is contained in:
@ -1,22 +1,38 @@
|
|||||||
|
import 'package:flutter/gestures.dart';
|
||||||
import 'package:flame/flame.dart';
|
import 'package:flame/flame.dart';
|
||||||
import 'package:flame/game.dart';
|
import 'package:flame/game.dart';
|
||||||
import 'package:flame/animation.dart' as flame_animation;
|
import 'package:flame/animation.dart' as flame_animation;
|
||||||
import 'package:flame/components/animation_component.dart';
|
import 'package:flame/components/animation_component.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
void main() => runApp(MyGame().widget);
|
void main() {
|
||||||
|
final game = MyGame();
|
||||||
|
runApp(game.widget);
|
||||||
|
Flame.util.addGestureRecognizer(TapGestureRecognizer()
|
||||||
|
..onTapDown = (TapDownDetails evt) {
|
||||||
|
game.addAnimation();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
class MyGame extends BaseGame {
|
class MyGame extends BaseGame {
|
||||||
|
final animation = flame_animation.Animation.sequenced('chopper.png', 4,
|
||||||
|
textureWidth: 48, textureHeight: 48, stepTime: 0.15);
|
||||||
|
|
||||||
MyGame() {
|
MyGame() {
|
||||||
_start();
|
_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addAnimation() {
|
||||||
|
final animationComponent = AnimationComponent(100, 100, animation, destroyOnFinish: true);
|
||||||
|
animationComponent.x = size.width / 2 - 50;
|
||||||
|
animationComponent.y = 200;
|
||||||
|
|
||||||
|
add(animationComponent);
|
||||||
|
}
|
||||||
|
|
||||||
void _start() async {
|
void _start() async {
|
||||||
final Size size = await Flame.util.initialDimensions();
|
final Size size = await Flame.util.initialDimensions();
|
||||||
|
|
||||||
final animation = flame_animation.Animation.sequenced('chopper.png', 4,
|
|
||||||
textureWidth: 48, textureHeight: 48, stepTime: 0.15);
|
|
||||||
|
|
||||||
final animationComponent = AnimationComponent(100, 100, animation);
|
final animationComponent = AnimationComponent(100, 100, animation);
|
||||||
animationComponent.x = size.width / 2 - 100;
|
animationComponent.x = size.width / 2 - 100;
|
||||||
animationComponent.y = 100;
|
animationComponent.y = 100;
|
||||||
|
|||||||
@ -5,8 +5,9 @@ import 'package:flame/animation.dart';
|
|||||||
|
|
||||||
class AnimationComponent extends PositionComponent {
|
class AnimationComponent extends PositionComponent {
|
||||||
Animation animation;
|
Animation animation;
|
||||||
|
bool destroyOnFinish;
|
||||||
|
|
||||||
AnimationComponent(double width, double height, this.animation) {
|
AnimationComponent(double width, double height, this.animation, { this.destroyOnFinish = false }) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
}
|
}
|
||||||
@ -22,6 +23,8 @@ class AnimationComponent extends PositionComponent {
|
|||||||
double textureY = 0.0,
|
double textureY = 0.0,
|
||||||
double textureWidth,
|
double textureWidth,
|
||||||
double textureHeight,
|
double textureHeight,
|
||||||
|
|
||||||
|
this.destroyOnFinish = false,
|
||||||
}) {
|
}) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
@ -38,6 +41,9 @@ class AnimationComponent extends PositionComponent {
|
|||||||
@override
|
@override
|
||||||
bool loaded() => animation.loaded();
|
bool loaded() => animation.loaded();
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool destroy() => destroyOnFinish && animation.isLastFrame;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
prepareCanvas(canvas);
|
prepareCanvas(canvas);
|
||||||
|
|||||||
Reference in New Issue
Block a user