Playing a sound function
Collisions with enemies and items are processed in the main game class. So, it's here that we will place the function to play sound effects. Add the following function declaration to Game.h
:
/** * Plays the given sound effect, with randomized parameters./ */ void PlaySound(sf::Sound& sound, sf::Vector2f position = { 0.f, 0.f });
This function takes two parameters: we take the sound that we want to play as a reference, to avoid an expensive copy, and we also include a parameter for the position where we want to play the sound. Note that we've given the position parameter a default value of { 0.f, 0.f }
. Therefore, it can be ignored should we wish to do so. We'll cover exactly what this parameter does when we create spatialized sounds.
Let's give this class a basic body for now to simply play the sound passed via the parameter:
// Plays the given sound effect, with randomized parameters. void Game::PlaySound(sf::Sound& sound, sf::Vector2f position) { // Play...