Tweens e Animazioni
Cos’è un Tween?
Un tween (from “in-between”) interpola valori tra uno stato iniziale e uno finale, creando animazioni fluide senza dover scrivere logica frame-by-frame.
Tween Basilare
// Muovi sprite da A a B
this.tweens.add({
targets: this.player,
x: 400,
y: 300,
duration: 1000, // millisecondi
ease: 'Power2'
});
Proprietà Animabili
this.tweens.add({
targets: sprite,
x: 400, // Posizione
y: 300,
scaleX: 2, // Scala
scaleY: 2,
rotation: Math.PI, // Rotazione (radianti)
alpha: 0.5, // Trasparenza
angle: 180, // Angolo (gradi)
duration: 1000
});
Easing Functions
// Linear (costante)
ease: 'Linear'
// Quadratic
ease: 'Power2' // o 'Quad'
ease: 'Power2.easeIn'
ease: 'Power2.easeOut'
ease: 'Power2.easeInOut'
// Cubic
ease: 'Power3' // o 'Cubic'
// Elastic (rimbalzo)
ease: 'Elastic'
ease: 'Elastic.easeOut'
// Bounce
ease: 'Bounce.easeOut'
// Back (supera il target poi torna)
ease: 'Back.easeOut'
// Sine
ease: 'Sine.easeInOut'
Opzioni Avanzate
this.tweens.add({
targets: sprite,
x: 400,
duration: 1000,
ease: 'Power2',
// Ripetizione
repeat: 3, // Ripeti 3 volte
repeat: -1, // Loop infinito
yoyo: true, // Torna indietro
// Delay
delay: 500, // Aspetta prima di iniziare
hold: 1000, // Pausa al target
repeatDelay: 200, // Pausa tra ripetizioni
// Callbacks
onStart: () => console.log('Started'),
onUpdate: () => console.log('Updating'),
onComplete: () => console.log('Completed'),
onYoyo: () => console.log('Yoyo'),
onRepeat: () => console.log('Repeat')
});
Tween Multipli
// Anima più oggetti insieme
this.tweens.add({
targets: [sprite1, sprite2, sprite3],
y: 300,
duration: 1000
});
// Anima più proprietà con timing diversi
this.tweens.add({
targets: sprite,
props: {
x: { value: 400, duration: 1000 },
y: { value: 300, duration: 2000 },
alpha: { value: 0, duration: 500, delay: 1500 }
}
});
Timeline
Sequenza di tweens:
const timeline = this.tweens.createTimeline();
timeline.add({
targets: sprite,
x: 400,
duration: 1000
});
timeline.add({
targets: sprite,
y: 300,
duration: 1000,
offset: 500 // Inizia 500ms dopo il precedente
});
timeline.add({
targets: sprite,
alpha: 0,
duration: 500
});
timeline.play();
Controllo Tween
// Salva riferimento
const tween = this.tweens.add({
targets: sprite,
x: 400,
duration: 2000,
paused: true // Inizia in pausa
});
// Controlli
tween.play();
tween.pause();
tween.resume();
tween.stop();
tween.restart();
// Velocità
tween.setTimeScale(2); // 2x velocità
// Controlla stato
if (tween.isPlaying()) {
// ...
}
Esempi Pratici
Pulsante Hover
const button = this.add.sprite(400, 300, 'button').setInteractive();
button.on('pointerover', () => {
this.tweens.add({
targets: button,
scaleX: 1.1,
scaleY: 1.1,
duration: 200,
ease: 'Back.easeOut'
});
});
button.on('pointerout', () => {
this.tweens.add({
targets: button,
scaleX: 1,
scaleY: 1,
duration: 200
});
});
Coin Collect
collectCoin(player, coin) {
this.tweens.add({
targets: coin,
y: coin.y - 50,
alpha: 0,
duration: 500,
ease: 'Power2',
onComplete: () => coin.destroy()
});
}
Camera Shake
// Scuoti camera
this.cameras.main.shake(200, 0.01);
// O con tween custom
this.tweens.add({
targets: this.cameras.main,
scrollX: '+=10',
duration: 50,
yoyo: true,
repeat: 5
});
Floating Text
showDamage(x, y, damage) {
const text = this.add.text(x, y, `-${damage}`, {
fontSize: '32px',
fill: '#ff0000'
});
this.tweens.add({
targets: text,
y: y - 100,
alpha: 0,
duration: 1000,
ease: 'Power2',
onComplete: () => text.destroy()
});
}
Pulse Effect
// Effetto pulsante
this.tweens.add({
targets: sprite,
scaleX: 1.2,
scaleY: 1.2,
duration: 500,
yoyo: true,
repeat: -1,
ease: 'Sine.easeInOut'
});
Path Following
// Crea path
const path = new Phaser.Curves.Path(100, 100);
path.lineTo(400, 100);
path.lineTo(400, 400);
path.lineTo(100, 400);
path.closePath();
// Segui path
this.tweens.add({
targets: sprite,
duration: 4000,
repeat: -1,
ease: 'Linear',
onUpdate: (tween) => {
const progress = tween.progress;
const point = path.getPoint(progress);
sprite.setPosition(point.x, point.y);
}
});
Chain Tweens
// Tween 1
this.tweens.add({
targets: sprite,
x: 400,
duration: 1000,
onComplete: () => {
// Tween 2 (dopo il primo)
this.tweens.add({
targets: sprite,
y: 300,
duration: 1000,
onComplete: () => {
// Tween 3
this.tweens.add({
targets: sprite,
alpha: 0,
duration: 500
});
}
});
}
});
Number Tweens
Anima valori numerici custom:
const data = { value: 0 };
this.tweens.add({
targets: data,
value: 100,
duration: 2000,
onUpdate: () => {
this.scoreText.setText(`Score: ${Math.floor(data.value)}`);
}
});
Best Practices
- Usa easing appropriato:
Power2per movimento,Elasticper effetti giocosi - Distruggi oggetti dopo tween: Usa
onCompleteper cleanup - Evita troppi tween simultanei: Impatta performance
- Usa timeline per sequenze: Più leggibile di callback annidati
- Salva riferimenti: Per controllare tween in corso
Conclusione
I tweens sono uno strumento potente per creare animazioni fluide con poco codice. Padroneggiare easing e timeline rende il gioco più dinamico e professionale.
