- 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:
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:
float distSqr = (bullet.Position - _player.Position).LengthSquared;
if(distSqr < 140.0f)
{
Audio.Play(_playerDieSound, 0.0f, 1.0f, 1.0f);
Game.SetStage(new TitleStage(Game));
}
Prev Page :: Back to Index