Move Particles to Dynamic Target
Demonstrates how to use onUpdate callbacks with particle moveTo properties to make particles track a moving target like the mouse pointer.
800 × 600
class Example extends Phaser.Scene
{
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('bg', 'assets/skies/gradient26.png');
this.load.image('ball', 'assets/demoscene/green_ball.png');
}
create ()
{
this.add.image(400, 300, 'bg');
let x = 400;
let y = 570;
this.input.on('pointermove', pointer => {
x = pointer.worldX;
y = pointer.worldY;
});
this.add.particles(0, 0, 'ball', {
x: { min: 300, max: 500 },
y: -32,
advance: 2000,
moveToX: {
onEmit: () => {
return x;
},
onUpdate: () => {
return x;
}
},
moveToY: {
onEmit: () => {
return y;
},
onUpdate: () => {
return y;
}
},
lifespan: 2000,
sortProperty: 'lifeT',
sortOrderAsc: true
});
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#000',
parent: 'game-container',
scene: Example
};
const game = new Phaser.Game(config);