Created
October 23, 2022 15:10
-
-
Save spydon/4cfe4f2204d1105aaa7b3004d2d52ad2 to your computer and use it in GitHub Desktop.
Draggable example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flame/components.dart'; | |
import 'package:flame/game.dart'; | |
import 'package:flame/input.dart'; | |
import 'package:flutter/material.dart' hide Draggable; | |
void main() { | |
runApp(GameWidget(game: DraggablesExample())); | |
} | |
class DraggablesExample extends FlameGame with HasDraggables { | |
late final DraggableEmber square; | |
@override | |
Future<void> onLoad() async { | |
add(square = DraggableEmber()); | |
add(DraggableEmber()..y = 350); | |
} | |
} | |
// Note: this component does not consider the possibility of multiple | |
// simultaneous drags with different pointerIds. | |
class DraggableEmber extends RectangleComponent with Draggable { | |
@override | |
bool debugMode = true; | |
DraggableEmber({Vector2? position}) | |
: super( | |
position: position ?? Vector2.all(100), | |
size: Vector2.all(100), | |
); | |
@override | |
void update(double dt) { | |
super.update(dt); | |
debugColor = isDragged && parent is DraggablesExample | |
? Colors.greenAccent | |
: Colors.purple; | |
} | |
@override | |
bool onDragUpdate(DragUpdateInfo info) { | |
if (parent is! DraggablesExample) { | |
return true; | |
} | |
position.add(info.delta.game); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment