Time for action – vanishing coins spawning particles
It is time now to practice our freshly acquired skills. The task is to add a particle effect to the game when the player collects coins. The coin will explode into a sprinkle of colorful stars when collected.
Start by declaring a particle system as filling the game scene, along with the particle painter definition:
ParticleSystem { id: coinParticles anchors.fill: parent // scene is the parent ImageParticle { source: "particle.png" colorVariation: 1 rotationVariation: 180 rotationVelocityVariation: 10 } }
Next, modify the definition of Coin to include an emitter:
Emitter { id: emitter system: coinParticles emitRate: 0 lifeSpan: 500 lifeSpanVariation: 100 velocity: AngleDirection { angleVariation: 180; magnitude: 10 } acceleration: AngleDirection { angle: 270; magnitude: 2 } }
Finally, the hit function has to be updated:
function hit() { emitter.burst(50) hitAnim.start() }