Day 6: Adding Animations, Sound Effects, and Background Music
Welcome to Day 6 of building your cross-platform mobile game with React Native and Phaser. Now that the core gameplay works, itβs time to add juice β visuals and audio that make your game feel polished and fun.
π§ What Youβll Learn
- How to animate sprites in Phaser
- How to play sound effects on events (e.g. collisions, power-ups)
- How to add and control background music
- How to preload and manage assets efficiently
π¨ Step 1: Add an Animated Player Sprite
Letβs replace the static player logo with a sprite sheet animation.
1. Preload a sprite sheet
In preload()
:
this.load.spritesheet('playerAnim', 'https://labs.phaser.io/assets/sprites/mushroom2.png', {
frameWidth: 32,
frameHeight: 32
});
2. Create an animation
In create()
:
this.anims.create({
key: 'wiggle',
frames: this.anims.generateFrameNumbers('playerAnim', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
player = this.physics.add.sprite(160, 400, 'playerAnim');
player.setCollideWorldBounds(true);
player.play('wiggle');
π Step 2: Add Sound Effects
1. Preload sounds in preload()
this.load.audio('hit', 'https://labs.phaser.io/assets/audio/SoundEffects/explosion.mp3');
this.load.audio('powerup', 'https://labs.phaser.io/assets/audio/SoundEffects/pickup.wav');
this.load.audio('music', 'https://labs.phaser.io/assets/audio/loops/SpaceHarrier.mp3');
2. Add to create()
:
hitSound = this.sound.add('hit');
powerupSound = this.sound.add('powerup');
bgMusic = this.sound.add('music', { loop: true, volume: 0.5 });
bgMusic.play();
π₯ Step 3: Play Sounds on Events
In hitEnemy()
:
hitSound.play();
In collectPowerup()
:
powerupSound.play();
π Optional: Stop Music on Game Over
In endGame()
:
if (bgMusic && bgMusic.isPlaying) {
bgMusic.stop();
}
Restart music in this.scene.restart()
if needed.
π± Step 4: Test Animations and Sounds
- Start your game.
- Watch the animated player sprite.
- Hear music play and sound effects trigger on events.
- Adjust
volume
orloop
settings as needed.
β What Youβve Achieved
- Integrated sprite sheet animations
- Triggered event-based sound effects
- Added looping background music
- Enhanced the feel and feedback of your mobile game
π Up Next
In Day 7, weβll focus on building multiple levels/scenes, and introduce a start menu and pause/resume functionality β essential for a production-ready mobile game.
Your game now looks and sounds great β letβs give it structure next!