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
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
Mystery Box Platformer MechanicA custom Arcade Physics Sprite that reacts to being hit from below, triggering a bump animation and spawning an item.Platformer Enemy Edge DetectionA sprite that uses an Arcade Physics sensor to detect the edges of platforms and automatically change direction.Static Physics Sprite ClassDemonstrates creating a custom class that extends an Arcade Physics Sprite to define a static level exit with a specific texture frame and origin.Treasure Box Game ObjectA custom Arcade Physics Sprite class representing a treasure box that changes its animation frame and triggers a key spawn when activated.