You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flame引擎中实现粒子淡出效果?

为Flame文本粒子添加淡出效果

当然可以实现,下面提供两种实用方法给文本粒子添加透明度淡出效果:

方法一:自定义带透明度动画的文本组件

直接给TextComponent添加生命周期内的透明度渐变逻辑,灵活度更高:

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/particles.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const FooApp());
}

class FooApp extends StatelessWidget {
  const FooApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: GameWidget.controlled(gameFactory: TextGame.new),
    );
  }
}

class TextGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    super.onLoad();

    await add(
      ButtonComponent(
        position: Vector2(size.x * 0.5, size.y * 0.5),
        onPressed: () {
          add(
            ParticleSystemComponent(
              position: Vector2(size.x * 0.5, size.y * 0.5),
              particle: AcceleratedParticle(
                lifespan: 2.0,
                speed: Vector2(0, 50),
                child: ComponentParticle(
                  component: _FadingText(text: "Taped!", lifespan: 2.0),
                ),
              ),
            ),
          );
        },
        button: TextComponent(
          text: 'B',
          textRenderer: TextPaint(
            style: const TextStyle(
              fontSize: 88,
              color: Colors.white,
            ),
          ),
        ),
        anchor: Anchor.center,
      ),
    );
  }
}

class _FadingText extends TextComponent {
  final double lifespan;
  double _timePassed = 0;
  final Tween<double> _opacityTween = Tween(begin: 1.0, end: 0.0);

  _FadingText({required super.text, required this.lifespan}) : super(anchor: Anchor.center);

  @override
  void update(double dt) {
    super.update(dt);
    _timePassed += dt;
    // 根据已流逝时间占生命周期的比例计算透明度
    final progress = _timePassed / lifespan;
    opacity = _opacityTween.transform(progress.clamp(0.0, 1.0));
    // 生命周期结束后自动移除组件
    if (_timePassed >= lifespan) removeFromParent();
  }
}

方法二:使用Flame内置的OpacityParticle

利用Flame提供的OpacityParticle组件,直接给子粒子绑定透明度变化曲线,代码更简洁:

// 仅修改按钮点击事件中的粒子部分
onPressed: () {
  add(
    ParticleSystemComponent(
      position: Vector2(size.x * 0.5, size.y * 0.5),
      particle: AcceleratedParticle(
        lifespan: 2.0,
        speed: Vector2(0, 50),
        child: OpacityParticle(
          // 用曲线控制透明度变化,这里用easeOut让淡出更自然
          opacity: CurvedParticle(
            curve: Curves.easeOut,
            child: ConstantParticle(
              value: 1.0,
              lifespan: 2.0,
            ),
          ),
          child: ComponentParticle(
            component: TextComponent(
              anchor: Anchor.center,
              text: "Taped!",
            ),
          ),
        ),
      ),
    ),
  );
}

说明

  • 两种方法都以粒子的lifespan(2秒)为时间周期,实现从完全不透明到完全透明的过渡。
  • 自定义组件的方式适合需要调整淡出逻辑的场景;内置粒子组件则适合快速实现标准淡出效果。

内容的提问来源于stack exchange,提问作者shingo.nakanishi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 21:43:13