本文共 4221 字,大约阅读时间需要 14 分钟。
这节我们通过前面学过的粒子相关知识,加上粒子组的用法,实现一个有意思的 Demo
第一步:
import QtQuick 2.9import QtQuick.Window 2.2import QtQuick.Particles 2.0Window { visible: true width: 680 height: 440 title: qsTr("Rocket..") Item{ anchors.fill: parent Rectangle { id: root anchors.fill: parent color: "#1F1F1F" } }}
第二步:
// ---- // 火箭的粒子画笔 ImageParticle{ id:rocketPainter system: particleSystem // 将粒子分组 groups: ['rocket'] // 火箭 Image 图片 source:"qrc:/new/preImg/" + "ufo.png" entryEffect: ImageParticle.None } // 火箭烟雾的粒子画笔 ImageParticle{ id:smokePainter system: particleSystem groups: ['smoke'] source:"qrc:/new/preImg/" + "particle.png" // alpha: 0.3 entryEffect: ImageParticle.None }
上面是火箭/火箭烟雾的粒子画笔!
第三步:
// ---- // 火箭的发射器 Emitter{ id:rocketEmitter // 定义发射的区域 anchors.bottom: parent.bottom width: parent.width // 60 的原因是只能从底部发射 height: 60 system: particleSystem // 将粒子发射到 rocket 粒子组中, // 火箭画笔将会绘制这些粒子 group: 'rocket' emitRate: 2 maximumEmitted: 4 lifeSpan: 4800 lifeSpanVariation: 400 size: 62 // 速度和方向 velocity: AngleDirection{ // 方向向上 angle: 270 // 速度大小 magnitude: 250 magnitudeVariation: 50 } // 加速度(速度向下模拟重力) acceleration: AngleDirection{ // 加速度方向向下 angle: 90 magnitude: 50 } // 跟踪器 Tracer{ color: "red" // 跟踪器是否可见 visible: root.tracer } } // 烟雾发射器 // 注意这是个跟踪发射器 TrailEmitter{ id:smokeEmitter system: particleSystem group: 'smoke' // 跟踪火箭 follow:'rocket' // 调整烟雾在火箭的位置 emitHeight: 0 emitWidth: 16 // 一次发射的粒子数 emitRatePerParticle: 100 lifeSpan: 200 size: 20 // 大小允许的变化范围 sizeVariation: 4 // 结束时候的大小(直接消失) endSize: 0 // 速度和方向 velocity: AngleDirection{ // 方向向下 angle: 90 // 速度大小 magnitude: 100 magnitudeVariation: 50 } // 跟踪器 Tracer{ color: "red" // 跟踪器是否可见 visible: root.tracer } }
第四步:
// 摩擦 Friction{ // 作用的对象当然是火箭而不是烟雾了. groups: ['rocket'] // 定义摩擦的区域 anchors.top: parent.top width: parent.width height: 120 system: particleSystem // 到每秒5像素的时候就不再摩擦 threshold: 5 // 摩擦强度 factor: 0.9 Tracer{ color:"red" visible: true } } // 紊流 // 【暂时下面代码失效】 Turbulence{ groups: ['rocket'] // 有 紊流 的区域 anchors.bottom: parent.bottom width: parent.width height: 100 system: particleSystem // 紊流 强度 strength: 95 Tracer{ color: "red" visible: true } }
可以看到摩擦/紊流都起作用了!
当 ufo 进入摩擦区域之后,速度减慢,然后由于向下的加速度的原因会向下降,最后耗到生命周期了就消失!
后续>>
转载地址:http://vyei.baihongyu.com/