Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Playing music when certain event happens, rather than in whole game.  (Read 2090 times)

0 Members and 1 Guest are viewing this topic.

Baig

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hi every one.
i am making a game in which i wanna use both sound and music. sound and music initialization is fine, no errors in loading etc. when ball does not hit the paddle, game over. in loop (of touching ball with paddle) i have put sound, which works fine. when game gets over, i wanna play a music too.

please guide me where to put that music. i don't want it to play in whole game, but just when the game gets over.

here is the code i have made so far.

    // Hit_Sound
        SoundBuffer hit_buffer;
        Sound hit;
        hit_buffer.loadFromFile("hit_sound.wav");
        hit.setBuffer(hit_buffer);

    // Fail_Music
        Music fail;
        fail.openFromFile("fail.ogg");
       
        while (window.isOpen())
        {

        // If does not collide with paddle then,
                else if (ball_bound.top >= 400)
                {
                        // fail.play();
                        state = 4;
                }
 
        window.clear();

        switch (state)
                {

        // game lost
                case 4:
                        window.draw(fail_1);
                        window.draw(fail_2);
         // fail.play();
                        }
               
        window.display();
}

i have tried fail.play, where i thought it would work. i have put it in comments for now. it does play but not continuous. listening to it feels like a very short part of it is playing again and again.

Please guide me where i am making mistake.


« Last Edit: February 16, 2015, 08:37:01 pm by Baig »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Playing music when certain event happens, rather than in whole game.
« Reply #1 on: February 15, 2015, 12:42:06 pm »
It's probably because you keep on calling fail.play() (which restarts the music every time), instead of doing just once.
Laurent Gomila - SFML developer

Baig

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Playing music when certain event happens, rather than in whole game.
« Reply #2 on: February 15, 2015, 03:09:22 pm »
Can you please guide me where should i use fail.play(). i am stuck with it. No idea how to proceed.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Playing music when certain event happens, rather than in whole game.
« Reply #3 on: February 15, 2015, 03:43:32 pm »
One idea among others:

        else if (ball_bound.top >= 400 && state != 4)
        {
            fail.play();
            state = 4;
        }
Laurent Gomila - SFML developer

Baig

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Playing music when certain event happens, rather than in whole game.
« Reply #4 on: February 15, 2015, 03:59:59 pm »
Thank you so much  Laurent! it worked. Thanks again for guiding me :)

 

anything