mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
Improving FlareComponent API and updating FlareFlutter dependency
This commit is contained in:
2
doc/examples/flare/.gitignore
vendored
2
doc/examples/flare/.gitignore
vendored
@ -68,3 +68,5 @@
|
||||
!**/ios/**/default.pbxuser
|
||||
!**/ios/**/default.perspectivev3
|
||||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
||||
|
||||
.flutter-plugins-dependencies
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
Example game application showcasing how to use Flare animations on Flame.
|
||||
|
||||
Flare animation used for the example: https://www.2dimensions.com/a/kautuk/files/flare/bob-minion/preview
|
||||
|
||||
The `main.dart` is a more generic example, test most of the features as `main_component.dart` is a specific example for the `FlareComponent` class.
|
||||
|
||||
74
doc/examples/flare/lib/main_component.dart
Normal file
74
doc/examples/flare/lib/main_component.dart
Normal file
@ -0,0 +1,74 @@
|
||||
import 'dart:ui';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/components/flare_component.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/position.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final game = MyGame();
|
||||
runApp(game.widget);
|
||||
}
|
||||
|
||||
class MyGame extends BaseGame with TapDetector, DoubleTapDetector {
|
||||
final TextConfig fpsTextConfig =
|
||||
const TextConfig(color: const Color(0xFFFFFFFF));
|
||||
|
||||
final paint = Paint()..color = const Color(0xFFE5E5E5E5);
|
||||
final List<String> _animations = ["Stand", "Wave", "Jump", "Dance"];
|
||||
int _currentAnimation = 0;
|
||||
FlareComponent flareComponent;
|
||||
|
||||
bool loaded = false;
|
||||
|
||||
MyGame() {
|
||||
_start();
|
||||
}
|
||||
|
||||
@override
|
||||
bool debugMode() => true;
|
||||
|
||||
@override
|
||||
void onTap() {
|
||||
cycleAnimation();
|
||||
}
|
||||
|
||||
@override
|
||||
void onDoubleTap() {
|
||||
flareComponent.width += 10;
|
||||
flareComponent.height += 10;
|
||||
|
||||
flareComponent.x -= 10;
|
||||
flareComponent.y -= 10;
|
||||
}
|
||||
|
||||
void cycleAnimation() {
|
||||
if (_currentAnimation == 3) {
|
||||
_currentAnimation = 0;
|
||||
} else {
|
||||
_currentAnimation++;
|
||||
}
|
||||
flareComponent.updateAnimation(_animations[_currentAnimation]);
|
||||
}
|
||||
|
||||
void _start() async {
|
||||
flareComponent = FlareComponent("assets/Bob_Minion.flr", "Stand", 306, 228);
|
||||
flareComponent.x = 50;
|
||||
flareComponent.y = 240;
|
||||
add(flareComponent);
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
|
||||
if (debugMode()) {
|
||||
fpsTextConfig.render(canvas, fps(120).toString(), Position(0, 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,6 +20,12 @@ class FlareComponent extends PositionComponent {
|
||||
});
|
||||
}
|
||||
|
||||
void updateAnimation(String animation) {
|
||||
if (loaded()) {
|
||||
_flareAnimation.updateAnimation(animation);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool loaded() => _flareAnimation != null;
|
||||
|
||||
@ -31,8 +37,20 @@ class FlareComponent extends PositionComponent {
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
if (_flareAnimation != null) {
|
||||
if (loaded()) {
|
||||
_flareAnimation.update(dt);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
set width(_width) {
|
||||
super.width = _width;
|
||||
if (loaded()) _flareAnimation.width = width;
|
||||
}
|
||||
|
||||
@override
|
||||
set height(_height) {
|
||||
super.height = _height;
|
||||
if (loaded()) _flareAnimation.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ dependencies:
|
||||
tiled: ^0.2.1
|
||||
convert: ^2.0.1
|
||||
flutter_svg: ^0.15.0
|
||||
flare_flutter: ^1.5.4
|
||||
flare_flutter: ^2.0.1
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user