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

Author Topic: Music Play  (Read 3449 times)

0 Members and 2 Guests are viewing this topic.

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Music Play
« on: March 26, 2017, 01:40:58 pm »
Hello, I want to play music when my player dies, I have this in Int main
 
int main()
{
        Music death;
        death.openFromFile("music/death.ogg");
and I want to put death.play(); here
while (window.isOpen())
        {

                float time = clock.getElapsedTime().asMicroseconds();
                if (p.life == true) gameTime = gameTimeClock.getElapsedTime().asSeconds();
                else {
                        p.sprite.setColor(Color(235, 65, 7));
                       
                        view.zoom(0.999f);
                        view.move(0, 0.03);
                        view.move(0.03, 0);
                }

but
if (p.life == true) gameTime = gameTimeClock.getElapsedTime().asSeconds();
                else {
death.play();
wouldn't work... Can you please tell me what to do in details, because I'm just starting to learn programming.

Some details: Visual studio 2013, win 10, and I have class Player in my code.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Music Play
« Reply #1 on: March 26, 2017, 02:28:35 pm »
wouldn't work
What does this mean? What does it do instead of what you expect it to do? Crash? Hear nothing? Error message?

Are you certain that the music file is opening correctly?
Remember to test the return value of openFromFile():
if (!death.openFromFile("music/death.ogg"))
{
    std::cerr << "\"music/death.ogg\" could not be opened!" << std::endl;
    return EXIT_FAILURE;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Music Play
« Reply #2 on: March 26, 2017, 10:40:49 pm »
I expect the music to play when p.life==true, it doesn't play, i hear nothing, yes I checked file destination, and it opens correctly, and I checked the file - it's not broken

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Music Play
« Reply #3 on: March 27, 2017, 03:40:58 am »
Well you aren't ever calling death.play() inside of the if statement checking p.life == true, so it won't ever play.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Music Play
« Reply #4 on: March 27, 2017, 12:27:54 pm »
I expect the music to play when p.life==true, it doesn't play, i hear nothing
If you expect it to play when p.life is true, it should be played from the block following the if, not in the code block following the else (plays when it is not true).

Note also that if the music has begun, telling it to play again will restart it from the beginning. If this happens every cycle, it's likely the music won't get far enough into the sound for anything to be heard.

yes I checked file destination, and it opens correctly, and I checked the file - it's not broken
I don't mean that you checked it manually or once but instead that you should check it every time it is opened. I even gave you the code that you can use :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything