Key Sprite Class

A custom Sprite class for a collectible item that demonstrates a floating tween animation, cross-scene audio triggering, and self-destruction.

1024 × 768
GitHub
import AUDIO_KEYS from "../audioKeys.js";
import SPRITE_KEYS from "../spriteKeys.js";

export class Key extends Phaser.GameObjects.Sprite
{
    moveY = 32;
    gameUiScene;

    constructor(scene, x, y)
    {
        super(scene, x, y, SPRITE_KEYS.FANTASY_TILES, 51);

        scene.add.existing(this);

        scene.tweens.add({
            targets: this,
            y: y - this.moveY,
            duration: 500,
            ease: 'Quad.easeOut',
            hold: 500,
            onComplete: () =>
            {
                this.destroy();
            }
        });

        this.gameUiScene = scene.scene.get('GameUi');
        this.gameUiScene.playAudio(AUDIO_KEYS.KEY);
    }
}

More in GameObjects13 examples