Files
Lukas Klingsbo 948f277e5d Fix gestures when isHud = true and camera is transformed (#843)
* Fix gestures when isHud = true and camera is transformed

* Use Info instead of Event everywhere
2021-06-09 23:47:59 +02:00

44 lines
1.0 KiB
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';
class ScrollGame extends BaseGame with ScrollDetector {
static const speed = 2000.0;
final _size = Vector2.all(50);
final _paint = BasicPalette.white.paint();
Vector2 position = Vector2.all(100);
Vector2? target;
@override
void onScroll(PointerScrollInfo info) {
target = position + info.scrollDelta.game * 5;
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(position.toPositionedRect(_size), _paint);
}
@override
void update(double dt) {
super.update(dt);
final target = this.target;
final ds = speed * dt;
if (target != null) {
if (position != target) {
final diff = target - position;
if (diff.length < ds) {
position.setFrom(target);
} else {
diff.scaleTo(ds);
position.setFrom(position + diff);
}
}
}
}
}