SFML community forums

Help => Audio => Topic started by: Ranzini on March 13, 2023, 02:14:22 pm

Title: Sound playing the moment the key is pressed
Post by: Ranzini on March 13, 2023, 02:14:22 pm
So i want a Sound to play, the moment the button is pressed.
With:

 if (Keyboard::isKeyPressed(Keyboard::Space))

the sound playes the moment i let go of the button.
Thanks in advance
Title: Re: Sound playing the moment the key is pressed
Post by: G. on March 13, 2023, 11:44:27 pm
Quote from: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Sound.php#a2953ffe632536e72e696fd880ced2532
...restarts it from beginning if it was it already playing
Don't call play() if it's already playing, you won't hear anything if you restart the sound every frame.

May want to use an event like the KeyPressed Event (https://www.sfml-dev.org/tutorials/2.5/window-events.php#the-keypressed-and-keyreleased-events) instead.