我刚刚点燃了火焰,我想在7秒内将一个组件从这个位置逐步移动到位置列表中.

到目前为止,我所做的是在不同的点之间有一个延迟,我想要一些流动的东西.

void animate({
required PositionComponent component,
required List<Vector2> newPositions,
required double duration,
void Function()? onComplete,
}) async {
    int index = 0;

    while (index < newPositions.length) {
      Vector2 newPosition = newPositions.elementAt(index);
      double progress = 0.0;

      while (progress <= 1.0) {
        component.position.lerp(newPosition, progress);

        await Future.delayed(const Duration(milliseconds: 100));

        progress += (1.0 / duration);
      }

      index++;
    }

    onComplete?.call();
  }

Flat版本(1.10.0).

提前谢谢!

推荐答案

你可以使用MoveAlongPathEffect:

// Starts at the components position, you can also use `absolute: false`
// in the `EffectController` if you want to define the points relative
// to the component.
final path = Path.moveTo(position.x, position.y);
for (final point in newPositions) {
  path.lineTo(point.x, point.y);
}

final moveEffect = MoveAlongPathEffect(
  path,
  EffectController(duration: 7),
  absolute: true,
);
add(moveEffect);

或者,如果您不想使用某种效果,可以使用Vector2的Flame扩展中的moveToTarget(或lerp)方法:

  /// Smoothly moves this [Vector2] in the direction [target] by a displacement
  /// given by a distance [ds] in that direction.
  ///
  /// It does not goes beyond target, regardless of [ds], so the final value
  /// is always [target].
  ///
  /// Note: [ds] is the displacement vector in units of the vector space. It is
  /// **not** a percentage (relative value).
  void moveToTarget(
    Vector2 target,
    double ds,
  )

Flutter相关问答推荐

如何创建一个按钮来在Ffltter Webview中转到上一页?

如何处理Flutter Riverpod异步通知器中的状态和数据库

为什么PUT方法没有发出任何响应?

为什么我的页面控制器在使用Riverpod时没有更改我的页面视图?

Ffmpeg_kit_fltter:^6.0.3版权问题

使索引[0]在ListViewBuilder中返回字符串1

如何在flutter中使用youtube_explod_start加载下一页

Flutter - Stripe:类型List 不是类型Map 的子类型? method_channel_stripe.dart 中的错误:263

无法应用插件com.chaquo.python

运行任何 flutter 命令都会显示无法安装 <版本>

在 Flutter 中,SpinEdit 叫什么?

是否可以在 flutter 应用程序中使用 jetpack compose?

面对Flutter 中的行和列问题

你如何在 Flutter 中组合两个数组?

Flutter 如何要求用户在应用程序中设置电话密码?

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中

出现错误;运算符[]不是为对象类型定义的?功能()'

Flutter 在气泡聊天消息上对齐时间戳文本

函数中的变量在 Dart 中应该有什么关键字?

如何将所有行项目平等地放在一行中?Flutter