Hello,
While experimenting and making a little game, i wanted to start the game with a first music and further it at some point with a second one.
The musics work fine if the function music.play() is called before the while(window.isOpen()), but if i put a music.play() inside this "while" the music will not..play correctly. It will stop at every move the mouse cursor make. Meaning if the mouse hasn't moved the music plays, if the cursor moves, it will restart.
I decided then, to put a bool variable inside the "while", and call the function music.play() after the while (after : while(window.isOpen() ) ). The condition would be for the bool variable to be true.
It does not work!
bool part2=0;
bool cond1=0;
bool part3=0;
...
..
///MUSIC
sf::Music Music2;
if(Music2.openFromFile("data/music2.ogg")==0)
{
return 1;
}
Music2.setVolume(30);
Music2.setLoop(true); ///
Music2.play();
//second music
sf::Music Music3;
if(Music3.openFromFile("data/music3.ogg")==0)
{
return 1;
}
Music3.setVolume(30);
Music3.setLoop(true); ///
Music3.play();
Music3.pause(); /// i tried here to make the music start before the while then pause it, to see if could just resume the music later.
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type== sf::Event::MouseButtonPressed && event.mouseButton.button ==sf::Mouse::Left)
{
....
...
{if(position.y<=446&&position.y>=263){if(money>=300&&weight<=450){part2=0;cond1=1;money=money-300;}}}
.....
Music3.play(); /// would do the problem discribed above
}
///LOGIC
....
...
// if(weight<=450&&cond1==0){part2=1;}
if(cond1==1){part3=1; part2=0;}//Music2.stop();Music2.play();} ... // it did not work here either
// if(Music2.getStatus()==sf::Sound::Stopped)
//{Music3.play();} // does not play
...
..
If you tell a music to play when it's already playing, it will restart from the beginning.
Hello Mr Hapax,
Yes indeed, i actually noticed it when the function was inside the while.
But after that, the function was called Soo fast that it had not the time to play, i thought the game could not handle 2 musics..
Here is the solution in case anyone browse this forum in 2022 :
if(Music2.getStatus()==sf::Sound::Stopped&&Music3.getStatus()==sf::Sound::Stopped)
{Music3.play();}
Meaning the music3 will not play only when the music2 has stopped, AND music3 will not play either if its not in a state of "stopped").
Thanks Hap