SFML community forums

Help => Audio => Topic started by: The Illusionist Mirage on September 01, 2013, 11:57:23 am

Title: Playing sound
Post by: The Illusionist Mirage on September 01, 2013, 11:57:23 am
Hello

I have made a custom mouse icon and I am using it through the methods -
Render_window_name.setMouseCursorVisible(false)
and
MouseSprite.setPosition(sf::Vector2f(sf::Mouse::getPosition(Render_window_name)));

I have also added a sound that is played everytime the user clicks the left button. Everything is smooth except that while I click the left button and hold it the sound keeps on playing. I want that it must be played only once when the left button is pressed, no matter for how much time I hold it.

How do I set up a sound using SFML so that it plays only once when the mouse is clicked.

Thanks
Title: Re: Playing sound
Post by: Nexus on September 01, 2013, 12:00:42 pm
Use events (sf::Event::MouseButtonPressed) instead of real-time input (sf::Mouse::isButtonPressed()).

And your position is wrong; use the sf::RenderWindow::map...() function to map correctly from pixel to world coordinates. Like this, your code won't break if you use a different view.
Title: Re: Playing sound
Post by: The Illusionist Mirage on September 01, 2013, 12:49:16 pm
Use events (sf::Event::MouseButtonPressed) instead of real-time input (sf::Mouse::isButtonPressed()).

Yea, thanks, it worked beautifully. :)

And your position is wrong; use the sf::RenderWindow::map...() function to map correctly from pixel to world coordinates. Like this, your code won't break if you use a different view.

I am making a pong clone and just need the mouse for navigating through menu and stuff. So do you really want that I should not use
MouseSprite.setPosition(sf::Vector2f(sf::Mouse::getPosition(Render_window_name)));
Title: Re: Playing sound
Post by: Nexus on September 01, 2013, 04:06:36 pm
It was a general advice. Your approach may work now, but you'll wonder why it breaks if the view changes.