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

Author Topic: Playing sound  (Read 3057 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Playing sound
« 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Playing sound
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Playing sound
« Reply #2 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)));

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Playing sound
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: