Collectible Physics Sprite
Demonstrates creating a custom Sprite class with Arcade Physics, featuring randomized bounce properties, collection logic, and physics body resetting.
1024 × 768
import AUDIO_KEYS from "../audioKeys.js";
import SPRITE_KEYS from "../spriteKeys.js";
export class Star extends Phaser.Physics.Arcade.Sprite
{
gameUiScene;
constructor (scene, x, y)
{
super(scene, x, y, SPRITE_KEYS.FANTASY_TILES, 54);
scene.add.existing(this);
scene.physics.add.existing(this);
this.setSize(24, 20);
this.setCollideWorldBounds(true);
this.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
this.gameUiScene = scene.scene.get('GameUi');
}
collect()
{
this.disableBody(true, true);
this.gameUiScene.playAudio(AUDIO_KEYS.STAR);
}
reset()
{
this.enableBody(true, this.x, 0, true, true);
}
}More in GameObjects13 examples
Custom Player Sprite ClassAn example of encapsulating player logic, such as movement, animations, and jumping, within a class that extends the Arcade Physics Sprite.Dynamic Texture PlatformsDemonstrates creating custom-sized platform sprites by stamping tile textures onto a Dynamic Texture and configuring them as static Arcade Physics bodies.Floating Score TextDemonstrates a custom BitmapText class that animates upwards and automatically destroys itself using a Tween, creating a classic floating score effect for points.Hamster Player ClassA demonstration of extending the Arcade Physics Sprite class to create a controllable character with encapsulated movement, jumping, and animation logic.