I want to play one of three sounds whenever a controllable square shape touches a rectangle.
I did something similar at the start of the program in which it will randomly pick 1 of 6 titles and display it.
string randomTitle1 = "Apples are under-rated!";
string randomTitle2 = "#datjaffa";
string randomTitle3 = "Cheese is epic!";
string randomTitle4 = "DjangoFTW!";
string randomTitle5 = "Bad games are under-rated!";
string randomTitle6 = "Pong by Finn Fallowfield - Copyright Digital Native Labs ltd.";
string chosenTitle = randomTitle6;
// EXAMPLE RANDOMIZER
srand(time(0));
int x = 1+(rand()%6);
cout << endl << endl << endl << endl;
cout << x << endl;
if(x == 1){
chosenTitle = randomTitle1;
}
else if(x == 2){
chosenTitle = randomTitle2;
}
else if(x == 3){
chosenTitle = randomTitle3;
}
else if(x == 4){
chosenTitle = randomTitle4;
}
else if(x == 5){
chosenTitle = randomTitle5;
}
else if(x == 6){
chosenTitle = randomTitle6;
}
else {
cout << "A fatal error occured!" << endl;
return 9;
}
// Creating the window
sf::RenderWindow window(sf::VideoMode(1200, 800), chosenTitle);
[/spoiler]
I would like to do the same for playing a sound, but it would have to randomly generate numbers all the time as I want the sound to continue to change randomly. When I tried, it would just take ages for it to play and then it would play the same sound everytime anyway. I tried putting a randomiser in a while loop but the program didn't played the music and no window appeared. Here is the code of the whole program so far:
I define all the sounds here:
// Sounds
sf::SoundBuffer ExplosionBuffer;
if(ExplosionBuffer.loadFromFile(resourcePath() + "explosion.wav") == 0){
return 1;
}
sf::SoundBuffer Hit1Buffer;
if (Hit1Buffer.loadFromFile(resourcePath() + randomSound1) == 0){
return 1;
}
sf::SoundBuffer Hit2Buffer;
if ( Hit2Buffer.loadFromFile(resourcePath()+ randomSound2) == 0){
return 1;
}
sf::SoundBuffer Hit3Buffer;
if (Hit3Buffer.loadFromFile(resourcePath()+ randomSound3) == 0){
return 1;
}
sf::Sound hit;
hit.setBuffer(Hit1Buffer);
hit.setLoop(false);
hit.setVolume(100);
sf::Sound hit2;
hit.setBuffer(Hit2Buffer);
hit.setLoop(false);
hit.setVolume(100);
sf::Sound hit3;
hit.setBuffer(Hit3Buffer);
hit.setLoop(false);
hit.setVolume(100);
sf::Sound explosion;
explosion.setBuffer(ExplosionBuffer);
explosion.setLoop(false);
explosion.setVolume(100);
This is the logic of the game
//LOGIC
if (rButton == true){
rectRotation++;
rectShape1.setRotation(rectRotation);
}
// Movement
if (rightButton == true){
xVelocity = 5;
}
if (leftButton == true){
xVelocity = -5;
}
if (upButton == true){
yVelocity = -5;
}
if (downButton == true){
yVelocity = 5;
}
if (rightButton == true && leftButton == true){
xVelocity = 0;
}
if (upButton == true && downButton == true){
yVelocity = 0;
}
if (rightButton == false && leftButton == false){
xVelocity = 0;
}
if (upButton == false && downButton == false){
yVelocity = 0;
}
// Move
rectShape1.move(xVelocity, 0);
if (rectShape1.getGlobalBounds().intersects(rectShape2.getGlobalBounds()) == true){
rectShape1.move(-xVelocity, 0);
[b] // This is also where I would play a sound[/b]
}
rectShape1.move(0, yVelocity);
if (rectShape1.getGlobalBounds().intersects(rectShape2.getGlobalBounds()) == true){
rectShape1.move(0, -yVelocity);
[b] // This is where I would play a sound[/b]
}
}
Any help would be much appreciated. I have just started learning so try to make your answer as simple and understandable as possible and try to include some code I could use. Thank you in advance!
Im using sfml 2.1 in Xcode on a mac running mavericks. FYI.