00
:
00
:
00
:
00
Corso SEO AI - Usa SEOEMAIL al checkout per il 30% di sconto

Sprites & Animazioni

Creare uno Sprite

Uno Sprite è un’immagine posizionata nello spazio di gioco che può muoversi, ruotare e animarsi.

```typescript create() { // x, y, key (nome dato nel preload) this.player = this.add.sprite(400, 300, ‘player’);

// Cambiare proprietà
this.player.setScale(2); // Doppio della grandezza
this.player.setFlipX(true); // Specchia orizzontalmente

} ```

Animazioni

Le animazioni usano gli Sprite Sheets caricati in precedenza.

```typescript create() { // Definiamo l’animazione this.anims.create({ key: ‘walk’, // Nome dell’animazione frames: this.anims.generateFrameNumbers(‘enemy’, { start: 0, end: 3 }), // Frame 0, 1, 2, 3 frameRate: 10, // 10 frame al secondo repeat: -1 // Loop infinito });

// Applichiamo l'animazione allo sprite
const enemy = this.add.sprite(200, 200, 'enemy');
enemy.play('walk');

} ```

Groups

Per gestire molti oggetti simili (es. proiettili o nemici), usa i Group.

```typescript this.bullets = this.add.group({ defaultKey: ‘bullet’, maxSize: 10 });

// Ottenere un membro dal gruppo (Object Pooling) const bullet = this.bullets.get(x, y); if (bullet) { bullet.setActive(true); bullet.setVisible(true); bullet.body.velocity.y = -300; } ```