Qt Quick Particles 示例 - 系统
这是 QML 粒子系统中使用 Affectors 的示例集。
这是有关在粒子系统中使用 Affectors 的 QML 小范例集。每个示例都是一个强调特定类型或特征的小 QML 文件。
动态比较比较了使用粒子系统和通过以下动态实例化图像类型的代码来获得类似效果。
Item { id: fakeEmitter function burst(number) { while (number > 0) { let item = fakeParticle.createObject(root) item.lifeSpan = Math.random() * 5000 + 5000 item.x = Math.random() * (root.width / 2) + (root.width / 2) item.y = 0 number-- } } Component { id: fakeParticle Image { id: container property int lifeSpan: 10000 width: 32 height: 32 source: "qrc:///particleresources/glowdot.png" PropertyAnimation on y { from: -16; to: root.height - 16; duration: container.lifeSpan; running: true } SequentialAnimation on opacity { running: true NumberAnimation { from: 0; to: 1; duration: 500 } PauseAnimation { duration: container.lifeSpan - 1000 } NumberAnimation { from: 1; to: 0; duration: 500 } ScriptAction { script: container.destroy(); } } } } }
请注意,图像对象无法随机着色。
开始和停止(Start and Stop)简单地设置了ParticleSystem 的运行和暂停状态。虽然系统在停止或暂停时不会执行任何模拟,但重新启动会从头开始重新模拟,而取消暂停则会从原来的位置恢复模拟。
定时组变化是一个突出显示ParticleGroup 类型的示例。虽然通常用字符串名称表示组就足够了,但还可以通过设置组的属性来实现额外的效果。第一个组有一个可变的持续时间,但始终会过渡到第二个组。
ParticleGroup { name: "fire" duration: 2000 durationVariation: 2000 to: {"splode":1} }
第二组有一个TrailEmitter ,而发射到第三组的持续时间是固定的。通过将TrailEmitter 作为ParticleGroup 的直接子代,它会自动选择跟随该组。
ParticleGroup { name: "splode" duration: 400 to: {"dead":1} TrailEmitter { group: "works" emitRatePerParticle: 100 lifeSpan: 1000 maximumEmitted: 1200 size: 8 velocity: AngleDirection {angle: 270; angleVariation: 45; magnitude: 20; magnitudeVariation: 20;} acceleration: PointDirection {y:100; yVariation: 20} } }
第三个组的直接子组是一个 "影响器",这使得 "影响器 "自动以这个组为目标。影响器意味着,只要粒子进入该组,就可以使用该粒子的 x、y 位置在另一个发射器上调用爆破函数。
ParticleGroup { name: "dead" duration: 1000 Affector { once: true onAffected: (x, y)=> worksEmitter.burst(400,x,y) } }
如果TrailEmitter 无法满足您对多个发射器的需求,您也可以动态创建发射器,同时仍然使用相同的ParticleSystem 和图像粒子。
for (var i = 0; i < 8; i++) { let obj = emitterComp.createObject(root) obj.x = x obj.y = y obj.targetX = Math.random() * 240 - 120 + obj.x obj.targetY = Math.random() * 240 - 120 + obj.y obj.life = Math.round(Math.random() * 2400) + 200 obj.emitRate = Math.round(Math.random() * 32) + 32 obj.go() }
请注意,使用TrailEmitter 更能实现彩虹长矛飞舞的效果。本示例中仅使用了动态发射器,以更简单地展示这一概念。
多重喷涂器展示了如何控制单个粒子的喷涂顺序。虽然没有严格定义一个 ImagePainter 中粒子的喷涂顺序,但ImageParticle 对象遵循正常的 Z 排序规则。 Qt Quick项目的正常 Z 排序规则。本示例允许您使用一对 ImageParticles 分别绘制同一逻辑粒子的不同部分,从而绘制黑色边框上方的粒子内部。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.