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

如何在Qt ParticleSystem中逐个依次发射不同图片粒子

如何在Qt ParticleSystem中逐个依次发射不同图片粒子

嘿,我懂你想要的效果——每次只发射一个特定图片的粒子,等这个粒子处理完后再触发下一个不同的,对吧?刚好之前做类似需求的时候摸索过几个可行的方案,给你参考下:

方案一:用Timer控制多个Emitter轮流激活

这个思路是给每个图片粒子配一个独立的Emitter,然后用定时器切换它们的激活状态,让每个Emitter每次只发射一个粒子。

首先调整你的Emitter配置,让它们默认不激活,且每次只发射一个粒子:

Window {
    width: 300
    height: 300
    Rectangle {
        anchors.fill: parent

        ParticleSystem {
            id: particleSystem
            anchors.centerIn: parent

            Age {
                system: particleSystem
                anchors.fill: parent
                once: true
                lifeLeft: 1000
                advancePosition: false
            }

            ImageParticle {
                groups: ["x"]
                source: "images/x.png"
            }
            ImageParticle {
                groups: ["y"]
                source: "images/y.png"
            }

            // 控制y粒子的Emitter
            Emitter {
                id: emitterY
                group: "y"
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                emitRate: 1    // 每次只发射1个
                duration: 1     // 发射一次就停止
                active: false   // 默认关闭
                velocity: AngleDirection { angle: 90; magnitude: 100 } // 示例速度,按需调整
            }

            // 控制x粒子的Emitter
            Emitter {
                id: emitterX
                group: "x"
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                emitRate: 1
                duration: 1
                active: false
                velocity: AngleDirection { angle: 90; magnitude: 100 }
            }
        }

        // 定时器切换Emitter
        Timer {
            id: switchTimer
            interval: 1000  // 每隔1秒切换一次,可自定义间隔
            running: true
            repeat: true
            property bool nextIsX: false // 标记下一个要发射的粒子类型

            onTriggered: {
                if (nextIsX) {
                    emitterX.restart() // 激活并发射x粒子
                } else {
                    emitterY.restart() // 激活并发射y粒子
                }
                nextIsX = !nextIsX // 切换下一次的粒子类型
            }
        }
    }
}

方案二:用ParticleSystem的burst()函数直接发射(更简洁)

Qt的ParticleSystem自带burst()方法,可以直接指定发射某个组的粒子数量,这样不用维护多个Emitter,代码更简洁。

Window {
    width: 300
    height: 300
    Rectangle {
        anchors.fill: parent

        ParticleSystem {
            id: particleSystem
            anchors.centerIn: parent

            Age {
                system: particleSystem
                anchors.fill: parent
                once: true
                lifeLeft: 1000
                advancePosition: false
            }

            ImageParticle {
                groups: ["x"]
                source: "images/x.png"
            }
            ImageParticle {
                groups: ["y"]
                source: "images/y.png"
            }

            // 这里不需要额外Emitter,直接用burst发射
        }

        Timer {
            id: burstTimer
            interval: 1000
            running: true
            repeat: true
            property int currentGroupIndex: 0
            property var particleGroups: ["x", "y"] // 按顺序定义要发射的粒子组

            onTriggered: {
                // 发射当前组的1个粒子
                particleSystem.burst(particleGroups[currentGroupIndex], 1)
                // 切换到下一个组,循环往复
                currentGroupIndex = (currentGroupIndex + 1) % particleGroups.length
            }
        }
    }
}

方案三:等前一个粒子消失后再发射下一个

如果你的需求是必须等上一个粒子完全消失后再发射下一个,而不是固定间隔,可以监听ParticleSystem的粒子数量变化:

Window {
    width: 300
    height: 300
    Rectangle {
        anchors.fill: parent

        ParticleSystem {
            id: particleSystem
            anchors.centerIn: parent

            Age {
                system: particleSystem
                anchors.fill: parent
                once: true
                lifeLeft: 1000
                advancePosition: false
            }

            ImageParticle {
                groups: ["x"]
                source: "images/x.png"
            }
            ImageParticle {
                groups: ["y"]
                source: "images/y.png"
            }
        }

        Timer {
            id: sequentialTimer
            interval: 0
            running: false
            repeat: false
            property int currentGroupIndex: 0
            property var particleGroups: ["x", "y"]

            onTriggered: {
                // 发射当前组的粒子
                particleSystem.burst(particleGroups[currentGroupIndex], 1)
                // 切换下一个要发射的组
                currentGroupIndex = (currentGroupIndex + 1) % particleGroups.length
            }
        }

        // 初始化启动,并且监听粒子数量变化
        Component.onCompleted: {
            sequentialTimer.start()
            // 当所有粒子消失后,启动下一次发射
            particleSystem.onParticleCountChanged: {
                if (particleSystem.particleCount === 0) {
                    sequentialTimer.start()
                }
            }
        }
    }
}

你可以根据自己的实际需求选对应的方案:固定间隔发射选前两种,要等粒子消失再发射就用第三种。

备注:内容来源于stack exchange,提问作者Hikosso

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.17 07:48:02