mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 01:18:38 +08:00
* 👌 Use `Offset` type directly in `JoystickAction.update` calculations (#631) * Move files to src and comply with the dart package layout convention * Fixing widgets example Co-authored-by: Serge Matveenko <lig@countzero.co> Co-authored-by: Erick Zanardo <erickzanardoo@gmail.com>
50 lines
938 B
Dart
50 lines
938 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.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);
|
|
}
|
|
}
|
|
}
|