mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-04 04:47:13 +08:00 
			
		
		
		
	* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
		
			
				
	
	
		
			84 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'package:flame/components.dart';
 | 
						|
import 'package:flame/extensions.dart';
 | 
						|
import 'package:flame/game.dart';
 | 
						|
import 'package:flame/input.dart';
 | 
						|
import 'package:flutter/material.dart' show Colors;
 | 
						|
 | 
						|
import '../../commons/ember.dart';
 | 
						|
 | 
						|
class DraggablesExample extends FlameGame with HasDraggables {
 | 
						|
  static const String description = '''
 | 
						|
    In this example we show you can use the `Draggable` mixin on
 | 
						|
    `PositionComponent`s. Drag around the Embers and see their position
 | 
						|
    changing.
 | 
						|
  ''';
 | 
						|
 | 
						|
  final double zoom;
 | 
						|
  late final DraggableSquare square;
 | 
						|
 | 
						|
  DraggablesExample({required this.zoom});
 | 
						|
 | 
						|
  @override
 | 
						|
  Future<void> onLoad() async {
 | 
						|
    await super.onLoad();
 | 
						|
    camera.zoom = zoom;
 | 
						|
    add(square = DraggableSquare());
 | 
						|
    add(DraggableSquare()..y = 350);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
// Note: this component does not consider the possibility of multiple
 | 
						|
// simultaneous drags with different pointerIds.
 | 
						|
class DraggableSquare extends Ember with Draggable {
 | 
						|
  @override
 | 
						|
  bool debugMode = true;
 | 
						|
 | 
						|
  DraggableSquare({Vector2? position})
 | 
						|
      : super(
 | 
						|
          position: position ?? Vector2.all(100),
 | 
						|
          size: Vector2.all(100),
 | 
						|
        );
 | 
						|
 | 
						|
  Vector2? dragDeltaPosition;
 | 
						|
 | 
						|
  @override
 | 
						|
  void update(double dt) {
 | 
						|
    super.update(dt);
 | 
						|
    debugColor = isDragged && parent is DraggablesExample
 | 
						|
        ? Colors.greenAccent
 | 
						|
        : Colors.purple;
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  bool onDragStart(int pointerId, DragStartInfo info) {
 | 
						|
    dragDeltaPosition = info.eventPosition.game - position;
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  bool onDragUpdate(int pointerId, DragUpdateInfo info) {
 | 
						|
    if (parent is! DraggablesExample) {
 | 
						|
      return true;
 | 
						|
    }
 | 
						|
    final dragDeltaPosition = this.dragDeltaPosition;
 | 
						|
    if (dragDeltaPosition == null) {
 | 
						|
      return false;
 | 
						|
    }
 | 
						|
 | 
						|
    position.setFrom(info.eventPosition.game - dragDeltaPosition);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  bool onDragEnd(int pointerId, _) {
 | 
						|
    dragDeltaPosition = null;
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  bool onDragCancel(int pointerId) {
 | 
						|
    dragDeltaPosition = null;
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
}
 |