Files
flame/doc/examples/gestures/lib/main_scroll.dart
Renan ccee9a466b Move files to src and comply with the dart package layout convention (#621)
* 👌 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>
2021-01-20 09:05:43 -03:00

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);
}
}
}