Hi!
I want my program to play a sound when a certain key is pressed. But the problem is that this happens too rapidly when a key is hold down.
I know that I can use set_keyboard_rate, but I also read that this only affects readkey() while I use key[KEY_NAME]. Can anyone give me some advice on this?
Here a little example:
while (!key[KEY_ESC]) {
if (key[KEY_UP]) {
play_sample(sample, 255, 128, 1000, 0);
}
}
The key array only tells you if a key is up or down. If you want to time that somehow, you'll have to do it yourself. That shouldn't be too hard, though.
So, with rest(time); everywhere? I think that's impossible, because I want to disable repeatment.
So, with rest(time); everywhere?
Of course not. That wouldn't really work, would it? I meant, do something like:
if(key[KEY_UP] && can_register_keypress) { play_sample(sample, 255, 128, 1000, 0); keypress_countdown = 60; //Waits for 60 loops can_register_keypress = false; } if(keypress_countdown > 0) keypress_countdown--; else if(!can_register_keypress) can_register_keypress = true;
</code>
That looks great! I'll try something this weekend...
Yeah, if you only want the sound to play once when a key is pressed, do what Thomas said.
If you only want it to play once when a key is pressed use readkey().
... no. That's wrong.
To quote X-G: stop giving shoddy advice.