1
Audio / multiple concurrent sound effects
« on: May 17, 2023, 09:27:04 am »
Hi All.
I am writing a game which requires the player to launch up to 9 missiles in quick succession. There is a sound effect for the launch of the missile and a sound effect for each explosion. The only way I can avoid the next sound effect cutting off the previous sound effect is for each missile to have two sound definitions, one for shoot and one for explosion. I cant use arrays as this will stop the sound playing and I cant use functions for the same reason. Relevant sections of code are as follows: This is for the explosions ...
... and so on, you get the idea. It does work but seems to be terrible code. I feel I have missed something very important which will avoid the need for this. Please can someone tell me if there is a better way to get this done?
Many thanks
Moon.
I am writing a game which requires the player to launch up to 9 missiles in quick succession. There is a sound effect for the launch of the missile and a sound effect for each explosion. The only way I can avoid the next sound effect cutting off the previous sound effect is for each missile to have two sound definitions, one for shoot and one for explosion. I cant use arrays as this will stop the sound playing and I cant use functions for the same reason. Relevant sections of code are as follows: This is for the explosions ...
sf::SoundBuffer missileBuffer0;
sf::SoundBuffer missileBuffer1;
sf::SoundBuffer missileBuffer2;
sf::SoundBuffer missileBuffer3;
sf::SoundBuffer missileBuffer4;
sf::SoundBuffer missileBuffer5;
sf::SoundBuffer missileBuffer6;
sf::SoundBuffer missileBuffer7;
sf::SoundBuffer missileBuffer8;
sf::SoundBuffer missileBuffer9;
sf::Sound missile0;
sf::Sound missile1;
sf::Sound missile2;
sf::Sound missile3;
sf::Sound missile4;
sf::Sound missile5;
sf::Sound missile6;
sf::Sound missile7;
sf::Sound missile8;
sf::Sound missile9;
... and this is the explosion hit sound effect playback.
switch (i)
{
case 0:
if (!missileBuffer0.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
}
missile0.setBuffer(missileBuffer0);
missile0.play();
break;
case 1:
if (!missileBuffer1.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
}
missile1.setBuffer(missileBuffer1);
missile1.play();
break;
case 2:
if (!missileBuffer2.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
...
sf::SoundBuffer missileBuffer1;
sf::SoundBuffer missileBuffer2;
sf::SoundBuffer missileBuffer3;
sf::SoundBuffer missileBuffer4;
sf::SoundBuffer missileBuffer5;
sf::SoundBuffer missileBuffer6;
sf::SoundBuffer missileBuffer7;
sf::SoundBuffer missileBuffer8;
sf::SoundBuffer missileBuffer9;
sf::Sound missile0;
sf::Sound missile1;
sf::Sound missile2;
sf::Sound missile3;
sf::Sound missile4;
sf::Sound missile5;
sf::Sound missile6;
sf::Sound missile7;
sf::Sound missile8;
sf::Sound missile9;
... and this is the explosion hit sound effect playback.
switch (i)
{
case 0:
if (!missileBuffer0.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
}
missile0.setBuffer(missileBuffer0);
missile0.play();
break;
case 1:
if (!missileBuffer1.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
}
missile1.setBuffer(missileBuffer1);
missile1.play();
break;
case 2:
if (!missileBuffer2.loadFromFile("rockhit.flac"))
{
cout << "No sound file";
...
... and so on, you get the idea. It does work but seems to be terrible code. I feel I have missed something very important which will avoid the need for this. Please can someone tell me if there is a better way to get this done?
Many thanks
Moon.