mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
This PR introduces a bridge package for Spine. https://user-images.githubusercontent.com/744771/236058480-75c8212b-98e6-4e3b-834a-c1a9d4de31a8.mp4
60 lines
1.5 KiB
Dart
60 lines
1.5 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/events.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame_spine/flame_spine.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await initSpineFlutter();
|
|
runApp(const GameWidget.controlled(gameFactory: SpineExample.new));
|
|
}
|
|
|
|
class SpineExample extends FlameGame with TapDetector {
|
|
late final SpineComponent spineboy;
|
|
|
|
final states = [
|
|
'walk',
|
|
'aim',
|
|
'death',
|
|
'hoverboard',
|
|
'idle',
|
|
'jump',
|
|
'portal',
|
|
'run',
|
|
'shoot',
|
|
];
|
|
|
|
int _stateIndex = 0;
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
// Load the Spineboy atlas and skeleton data from asset files
|
|
// and create a SpineComponent from them, scaled down and
|
|
// centered on the screen
|
|
spineboy = await SpineComponent.fromAssets(
|
|
atlasFile: 'assets/spineboy.atlas',
|
|
skeletonFile: 'assets/spineboy-pro.skel',
|
|
scale: Vector2(0.4, 0.4),
|
|
anchor: Anchor.center,
|
|
position: Vector2(size.x / 2, size.y / 2),
|
|
);
|
|
|
|
// Set the "walk" animation on track 0 in looping mode
|
|
spineboy.animationState.setAnimationByName(0, 'walk', true);
|
|
await add(spineboy);
|
|
}
|
|
|
|
@override
|
|
void onTap() {
|
|
_stateIndex = (_stateIndex + 1) % states.length;
|
|
spineboy.animationState.setAnimationByName(0, states[_stateIndex], true);
|
|
}
|
|
|
|
@override
|
|
void onDetach() {
|
|
// Dispose the native resources that have been loaded for spineboy.
|
|
spineboy.dispose();
|
|
}
|
|
}
|