mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +08:00
Update min Dart constraint to 3.8, which will enable us to use the fancier collection literals. This requires bumping the min Flutter version as well: <img width="1892" height="1122" alt="image" src="https://github.com/user-attachments/assets/7c7b07fc-4d96-4987-824d-9a7133ecfb85" />
67 lines
1.4 KiB
Dart
67 lines
1.4 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/events.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame/input.dart';
|
|
import 'package:flame/parallax.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
runApp(GameWidget(game: SpaceShooterGame()));
|
|
}
|
|
|
|
class SpaceShooterGame extends FlameGame with PanDetector {
|
|
late Player player;
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
final parallax = await loadParallaxComponent(
|
|
[
|
|
ParallaxImageData('stars_0.png'),
|
|
ParallaxImageData('stars_1.png'),
|
|
ParallaxImageData('stars_2.png'),
|
|
],
|
|
baseVelocity: Vector2(0, -5),
|
|
repeat: ImageRepeat.repeat,
|
|
velocityMultiplierDelta: Vector2(0, 5),
|
|
);
|
|
add(parallax);
|
|
|
|
player = Player();
|
|
add(player);
|
|
}
|
|
|
|
@override
|
|
void onPanUpdate(DragUpdateInfo info) {
|
|
player.move(info.delta.global);
|
|
}
|
|
}
|
|
|
|
class Player extends SpriteAnimationComponent
|
|
with HasGameReference<SpaceShooterGame> {
|
|
Player()
|
|
: super(
|
|
size: Vector2(100, 150),
|
|
anchor: Anchor.center,
|
|
);
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
await super.onLoad();
|
|
|
|
animation = await game.loadSpriteAnimation(
|
|
'player.png',
|
|
SpriteAnimationData.sequenced(
|
|
amount: 4,
|
|
stepTime: 0.2,
|
|
textureSize: Vector2(32, 48),
|
|
),
|
|
);
|
|
|
|
position = game.size / 2;
|
|
}
|
|
|
|
void move(Vector2 delta) {
|
|
position.add(delta);
|
|
}
|
|
}
|