Caricamento Assets
Il Metodo preload()
Phaser ha un Loader integrato che gestisce il caricamento asincrono delle risorse.
Tutto ciò che metti in preload() verrà scaricato prima che la scena inizi.
```typescript preload() { // Immagine singola this.load.image(‘player’, ‘assets/player.png’);
// Sprite Sheet (immagine con più frame)
// frameWidth e frameHeight definiscono la dimensione di ogni frame
this.load.spritesheet('enemy', 'assets/enemy_sheet.png', {
frameWidth: 32,
frameHeight: 32
});
// Audio
this.load.audio('jump', ['assets/jump.mp3', 'assets/jump.ogg']);
// JSON (es. dati di livello)
this.load.json('levelData', 'assets/levels/1.json');
} ```
Loading Bar
Mentre carica, puoi mostrare una barra di progresso per informare l’utente.
```typescript preload() { const width = this.cameras.main.width; const height = this.cameras.main.height;
const loadingText = this.make.text({
x: width / 2,
y: height / 2 - 50,
text: 'Caricamento...',
style: {
font: '20px monospace',
fill: '#ffffff'
}
}).setOrigin(0.5, 0.5);
this.load.on('progress', (value) => {
// value va da 0 a 1
console.log(value);
});
this.load.on('complete', () => {
loadingText.destroy();
});
// ... i tuoi this.load.image() ...
} ```