火焰引擎,可拖动组件不起作用

Flame-engine, Draggable component doesn't work

我是 flutter 世界的新手,尤其是火焰引擎。也许这是一个简单的问题。但目前我无法弄清楚。所以请帮助我!

主要问题是我的 Draggable 组件无法正常工作,所有事件处理函数似乎都无法正常工作。你有什么想法吗?

可拖动组件代码:

import 'package:flame/components.dart';
import 'package:flame/input.dart';

// Zooh bolomjtoi component
class DrawerComponent extends PositionComponent with Draggable {
  late final mainRectangle;

  Vector2? dragDeltaPosition;
  bool get isDragging => dragDeltaPosition != null;

  @override
  Future<void>? onLoad() async {
    final bg = await Sprite.load('drawer.png');
    final localSize = Vector2(350, 210);
    final mainRectangle = SpriteComponent(size: localSize, sprite: bg);
    mainRectangle.position = isDragging ? (dragDeltaPosition)! : Vector2(0, 0);
    add(mainRectangle);
  }

  @override
  bool onDragStart(DragStartInfo info) {
    print("Drag ehelleee..");
    dragDeltaPosition = info.eventPosition.game - position;
    return false;
  }

  @override
  bool onDragUpdate(DragUpdateInfo info) {
    print("Drag update..");
    if (isDragging) {
      final localCoords = info.eventPosition.game;
      position.setFrom(localCoords - dragDeltaPosition!);
    }
    return false;
  }

  @override
  bool onDragEnd(DragEndInfo info) {
    print("Drag end...");
    dragDeltaPosition = null;
    return false;
  }

  @override
  bool onDragCancel() {
    dragDeltaPosition = null;
    return false;
  }
}

主要 FlameGame class:

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:furniture/drawer.dart';

class Core extends FlameGame with HasDraggables {
  late final SpriteComponent mainRectangle;

  @override
  Future<void>? onLoad() async {
    final bg = await Sprite.load('example.jpeg');
    final localSize = Vector2(super.size.x * 0.7, super.size.y * 0.6);
    final mainRectangle = SpriteComponent(size: localSize, sprite: bg);

    mainRectangle.position = Vector2(super.size.x * 0.15, super.size.y * 0.2);
    mainRectangle.angle = 0;
    add(mainRectangle);
    add(DrawerComponent());
  }
}

Flutter 主class:

import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:furniture/core.dart';

void main() {
  final game = Core();
  runApp(GameWidget(game: game));
}

我认为你的问题出在你的 onLoad() 因为你没有正确声明值,我有这个例子给你:

class Sushi extends SpriteComponent with Draggable, HasGameRef<JapaneseChef>{
  /// Sushi constructor
  Sushi( this._img, this._position, this._isMove, this._isDraggable,){
    position = _position;
    size = Vector2(250,250);
  }

  late final String _img;
  late final Vector2 _position;
  late Vector2 _dragDeltaPosition;
  late final bool _isMove;
  late final bool _isDraggable;

  /// Get delta position
  Vector2 get dragDeltaPosition => _dragDeltaPosition;

  set dragDeltaPosition(Vector2? dragDeltaPosition) {
    if(dragDeltaPosition != null){
      _dragDeltaPosition = dragDeltaPosition;
    }
  }

  @override
  Future<void>? onLoad() async{
    final spriteSushi = await Sprite.load(_img);
    sprite = spriteSushi;
    changePriorityWithoutResorting(5);
    return super.onLoad();
  }

  @override
  void update(double dt) {
    if (_isMove) {
      position.x -= 0.6;
      if (position.x < -size.x) {
        remove(this);
      }
    }

    super.update(dt);
  }

  @override
  bool onDragStart(int pointerId, DragStartInfo info) {
    if(_isDraggable) {
      dragDeltaPosition = info.eventPosition.game - position;
    }
    return false;
  }

  @override
  bool onDragCancel(int pointerId) {
    dragDeltaPosition = null;
    return super.onDragCancel(pointerId);
  }

  @override
  bool onDragUpdate(int pointerId, DragUpdateInfo info) {
    if (parent is! JapaneseChef) {
      return true;
    }
    final dragDeltaPosition = this.dragDeltaPosition;

    position.setFrom(info.eventPosition.game - dragDeltaPosition);
    return false;
  }

  @override
  bool onDragEnd(int pointerId, DragEndInfo info) {
    dragDeltaPosition = null;
    return false;
  }

}

另外,你可以看看Flame引擎的例子

Example result