mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +08:00
* Fix rendering of children * Game loop handles other restore * Properly propagate onMount and onRemove to children * Use BaseGame on gestures to minimize confusion * Fix linting * All children don't need preparation * Add composability example * gameRef might not be defined * Add mustCallSuper * isMounted on game * Remove unused gameRef argument * Made isMounted only modifiable by the component * Move dartdoc to public isMounted * Fix formatting
51 lines
993 B
Dart
51 lines
993 B
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame/gestures.dart';
|
|
import 'package:flame/palette.dart';
|
|
import 'package:flame/extensions/vector2.dart';
|
|
import 'package:flame/extensions/offset.dart';
|
|
|
|
void main() {
|
|
final game = MyGame();
|
|
runApp(
|
|
GameWidget(
|
|
game: game,
|
|
),
|
|
);
|
|
}
|
|
|
|
class MyGame extends BaseGame with ScrollDetector {
|
|
static const SPEED = 200;
|
|
|
|
Vector2 position = Vector2(0, 0);
|
|
Vector2 target;
|
|
|
|
@override
|
|
void onScroll(event) {
|
|
target = position - event.scrollDelta.toVector2();
|
|
}
|
|
|
|
@override
|
|
void render(Canvas canvas) {
|
|
super.render(canvas);
|
|
canvas.drawRect(
|
|
Rect.fromLTWH(
|
|
position.x,
|
|
position.y,
|
|
50,
|
|
50,
|
|
),
|
|
BasicPalette.white.paint,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void update(double dt) {
|
|
super.update(dt);
|
|
if (target != null) {
|
|
final dir = (target - position).normalized();
|
|
position += dir * (SPEED * dt);
|
|
}
|
|
}
|
|
}
|