Arcade
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Step 8: SFX
  • Lastly, we'll add a couple sounds.
  • Get a sound for when the player walks (player_walk.wav) and a sound for when a bullet hits the player (player_die.wav) and add them to a new Resources/sounds/ folder.
  • Create a Sound variable (_walkSound) in Player.cs, then override OnAudioLoaded and save the loaded sfx:
protected override void OnLoadAudio(Audio audio)
{
_walkSound = audio.GetSound("Resources", "sounds", "player_walk");
}
  • Add some logic to Player's update method so the sound plays while walking:
// play a sound when our animation frame changes while walking
if(!_moveAxis.IsZero)
{
if(_walkAnim.Frame == 0 && _currentAnimFrame == 1)
Stage.Audio.Play(_walkSound, 0.0f, 1.0f, 1.0f);
_currentAnimFrame = _walkAnim.Frame;
}

  • In GameStage.cs, load the new variable _playerDieSound in the OnEnter method:
_playerDieSound = Audio.GetSound("Resources", "sounds", "player_die");
  • and play it when the player dies:
// check distance from each bullet to the player
float distSqr = (bullet.Position - _player.Position).LengthSquared;
if(distSqr < 140.0f)
{
// play death sound
Audio.Play(_playerDieSound, 0.0f, 1.0f, 1.0f);
// restart game
Game.SetStage(new TitleStage(Game));
}

Prev Page :: Back to Index