Particles
Sistema Particellare
Il sistema particellare di Phaser permette di creare effetti visivi come esplosioni, fumo, pioggia, neve e molto altro.
Particle Emitter Basilare
preload() {
this.load.image('particle', 'assets/particle.png');
}
create() {
// Crea emitter
const particles = this.add.particles(400, 300, 'particle', {
speed: 100,
scale: { start: 1, end: 0 },
blendMode: 'ADD'
});
}
Configurazione Emitter
const emitter = this.add.particles(x, y, 'particle', {
// Velocità
speed: { min: 100, max: 200 },
speedX: { min: -100, max: 100 },
speedY: { min: -200, max: -100 },
// Accelerazione
accelerationY: 300,
// Angolo
angle: { min: 0, max: 360 },
// Scala
scale: { start: 1, end: 0 },
// Rotazione
rotate: { min: 0, max: 360 },
// Trasparenza
alpha: { start: 1, end: 0 },
// Tint (colore)
tint: 0xff0000,
// Durata vita
lifespan: 2000,
// Quantità
quantity: 10,
frequency: 100, // Emetti ogni 100ms
// Blend mode
blendMode: 'ADD'
});
Emissione
// Emissione continua
const emitter = this.add.particles(x, y, 'particle', {
speed: 100,
lifespan: 1000,
frequency: 50
});
// Burst (esplosione singola)
const emitter = this.add.particles(x, y, 'particle', {
speed: 200,
lifespan: 1000
});
emitter.explode(20); // Emetti 20 particelle
// Emissione on-demand
emitter.emitParticle(10); // Emetti 10 particelle
Forme di Emissione
// Punto
emitZone: {
type: 'random',
source: new Phaser.Geom.Point(0, 0)
}
// Cerchio
emitZone: {
type: 'random',
source: new Phaser.Geom.Circle(0, 0, 50)
}
// Rettangolo
emitZone: {
type: 'random',
source: new Phaser.Geom.Rectangle(-50, -50, 100, 100)
}
// Edge (bordo cerchio)
emitZone: {
type: 'edge',
source: new Phaser.Geom.Circle(0, 0, 100),
quantity: 32
}
Death Zone
Area dove le particelle vengono distrutte:
const emitter = this.add.particles(400, 300, 'particle', {
speed: 100,
lifespan: 5000,
deathZone: {
type: 'onEnter',
source: new Phaser.Geom.Circle(400, 300, 200)
}
});
Follow Target
// Particelle seguono sprite
const emitter = this.add.particles(0, 0, 'particle', {
speed: 50,
lifespan: 500,
scale: { start: 0.5, end: 0 }
});
emitter.startFollow(this.player);
// Offset
emitter.startFollow(this.player, 0, -20); // 20px sopra
Esempi Pratici
Esplosione
createExplosion(x, y) {
const emitter = this.add.particles(x, y, 'particle', {
speed: { min: 200, max: 400 },
angle: { min: 0, max: 360 },
scale: { start: 1, end: 0 },
alpha: { start: 1, end: 0 },
lifespan: 600,
blendMode: 'ADD',
tint: [0xff0000, 0xff8800, 0xffff00]
});
emitter.explode(30);
// Distruggi dopo l'esplosione
this.time.delayedCall(1000, () => emitter.destroy());
}
Fuoco
const fire = this.add.particles(400, 500, 'particle', {
speed: { min: 20, max: 50 },
angle: { min: -100, max: -80 },
scale: { start: 0.5, end: 0 },
alpha: { start: 1, end: 0 },
lifespan: 1000,
frequency: 50,
tint: [0xff0000, 0xff8800, 0xffff00],
blendMode: 'ADD'
});
Pioggia
const rain = this.add.particles(0, 0, 'raindrop', {
x: { min: 0, max: 800 },
y: -10,
speedY: { min: 300, max: 500 },
accelerationY: 50,
lifespan: 3000,
frequency: 10,
scale: { min: 0.5, max: 1 },
alpha: { min: 0.3, max: 0.6 }
});
Scia (Trail)
const trail = this.add.particles(0, 0, 'particle', {
speed: 0,
lifespan: 500,
scale: { start: 0.5, end: 0 },
alpha: { start: 0.8, end: 0 },
frequency: 20
});
trail.startFollow(this.player);
Stelle (Background)
const stars = this.add.particles(0, 0, 'star', {
x: { min: 0, max: 800 },
y: { min: 0, max: 600 },
speed: 0,
scale: { min: 0.1, max: 0.5 },
alpha: { min: 0.3, max: 1 },
lifespan: 0, // Infinite
quantity: 100,
frequency: -1 // Emetti solo all'inizio
});
Sangue (Hit Effect)
createBloodSplatter(x, y) {
const blood = this.add.particles(x, y, 'blood', {
speed: { min: 100, max: 200 },
angle: { min: 0, max: 360 },
scale: { start: 1, end: 0.5 },
alpha: { start: 1, end: 0 },
lifespan: 800,
gravityY: 300,
tint: 0x8B0000
});
blood.explode(15);
this.time.delayedCall(1000, () => blood.destroy());
}
Particle Manager
Gestisci multiple emitter:
create() {
// Crea texture per particelle
const graphics = this.add.graphics();
graphics.fillStyle(0xffffff);
graphics.fillCircle(4, 4, 4);
graphics.generateTexture('particle', 8, 8);
graphics.destroy();
// Manager globale
this.particleManager = this.add.particles(0, 0, 'particle');
// Crea emitter dal manager
this.fireEmitter = this.particleManager.createEmitter({
speed: 50,
lifespan: 1000,
tint: 0xff0000
});
this.smokeEmitter = this.particleManager.createEmitter({
speed: 30,
lifespan: 2000,
tint: 0x888888
});
}
Controllo Runtime
// Start/Stop
emitter.start();
emitter.stop();
// Pausa
emitter.pause();
emitter.resume();
// Cambia proprietà
emitter.setSpeed({ min: 50, max: 100 });
emitter.setScale({ start: 2, end: 0 });
emitter.setAlpha({ start: 1, end: 0 });
// Posizione
emitter.setPosition(x, y);
// Frequenza
emitter.setFrequency(100);
emitter.setQuantity(5);
Performance
// Limita particelle massime
const emitter = this.add.particles(x, y, 'particle', {
speed: 100,
lifespan: 1000,
maxParticles: 100 // Max 100 particelle attive
});
// Usa texture piccole
// Preferisci 8x8 o 16x16 pixel
// Distruggi emitter quando non serve
emitter.destroy();
Best Practices
- Texture piccole: 8x8 o 16x16 pixel per performance
- Limita particelle: Usa
maxParticlesper evitare lag - Distruggi emitter: Cleanup dopo effetti one-shot
- Blend modes:
ADDper effetti luminosi,NORMALper default - Riusa emitter: Invece di crearne di nuovi
Conclusione
Il sistema particellare di Phaser è potente e flessibile. Con creatività puoi creare effetti visivi impressionanti che arricchiscono l’esperienza di gioco.
