Mystery Box Platformer Mechanic
A custom Arcade Physics Sprite that reacts to being hit from below, triggering a bump animation and spawning an item.
1024 × 768
import SPRITE_KEYS from "../spriteKeys.js";
export class MysteryBox extends Phaser.Physics.Arcade.Sprite
{
isActivated = false;
moveY = 16;
constructor (scene, x, y)
{
super(scene, x, y, SPRITE_KEYS.FANTASY_TILES, 52);
this.setOrigin(0, 0);
scene.add.existing(this);
scene.physics.add.existing(this, true);
}
activate(gameObject)
{
if (this.isActivated || !this.body.touching.down || !gameObject.isPlayer || gameObject.y < this.y) return;
this.isActivated = true;
this.setTint(0x666666);
this.scene.addCoin(this.x + (this.width*0.5), this.y - this.height + (this.height*0.5));
this.scene.tweens.add({
targets: this,
y: this.y - this.moveY,
duration: 100,
ease: 'Quad.easeOut',
yoyo: true,
});
}
}More in GameObjects13 examples
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.Animated Coin Collection EffectDemonstrates a custom Sprite class that manages its own spin animation, yoyo tween movement, sound effects, and self-destruction upon completion.