Files
flame/packages/flame_forge2d/example/lib/draggable_sample.dart
Lukas Klingsbo 27adda17b7 fix!: Remove pointerId from Draggable callbacks (#1313)
Since pointerId is already handled by the handle* methods we don't have to expose it in the onDrag* methods, this conforms with the standard set in Tappable.
2022-01-18 23:22:10 +01:00

47 lines
1.2 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/input.dart';
import 'package:flame_forge2d/forge2d_game.dart';
import 'package:flutter/material.dart' hide Draggable;
import 'balls.dart';
import 'boundaries.dart';
class DraggableSample extends Forge2DGame with HasDraggables {
DraggableSample() : super(gravity: Vector2.all(0.0));
@override
Future<void> onLoad() async {
await super.onLoad();
final boundaries = createBoundaries(this);
boundaries.forEach(add);
final center = screenToWorld(camera.viewport.effectiveSize / 2);
add(DraggableBall(center));
}
}
class DraggableBall extends Ball with Draggable {
DraggableBall(Vector2 position) : super(position, radius: 5) {
originalPaint = Paint()..color = Colors.amber;
paint = originalPaint;
}
@override
bool onDragStart(DragStartInfo info) {
paint = randomPaint();
return true;
}
@override
bool onDragUpdate(DragUpdateInfo info) {
final worldDelta = Vector2(1, -1)..multiply(info.delta.game);
body.applyLinearImpulse(worldDelta * 1000);
return true;
}
@override
bool onDragEnd(DragEndInfo info) {
paint = originalPaint;
return true;
}
}