Hi all!
I want different music for my game locations but I can't come up with a clever way of stopping the music of the previous location before starting the next location's music...
So, my code so far plays the first song in location1 then plays both songs together when I move to location2!
Please see my code below:
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace std;
int main()
{
sf::RenderWindow gameWindow( sf::VideoMode( 800, 600, 32 ), "Hello, World!" );
sf::Image Image, Image2;
if ( !Image.LoadFromFile( "DN1.png" ) )
cout << "Picture load error..." << endl;
sf::Sprite sprite1, sprite2;
sprite1.SetImage( Image );
if ( !Image2.LoadFromFile( "DN2.png" ) )
cout << "Picture load error..." << endl;
sprite2.SetImage( Image2 );
sprite1.SetPosition( 200.f, 250.f );
sprite2.SetPosition( 600.f, 250.f );
sf::Music music1, music2;
if ( !music1.OpenFromFile( "T1.ogg" ) )
cout << "No music 1..." << endl;
if ( !music2.OpenFromFile( "T2.ogg" ) )
cout << "No music 2..." << endl;
while ( gameWindow.IsOpened() )
{
sf::Event Event;
while ( gameWindow.GetEvent( Event ) )
{
// Window closed
if ( Event.Type == sf::Event::Closed )
gameWindow.Close();
// Escape key pressed
if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Escape ) )
gameWindow.Close();
// Left key pressed
if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Right ) )
{
gameWindow.Clear();
music1.Play();
gameWindow.Draw( sprite1 );
gameWindow.Display();
} // end if
// Right key pressed
if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Left ) )
{
gameWindow.Clear();
music2.Play();
gameWindow.Draw( sprite2 );
gameWindow.Display();
} // end if
} // end while
} // end while
} // end main