By salin
Date 2008-11-09 10:06
Hi.
I'm trying to implement the following behaviour. I want to call a method once when a key is pressed (for movement) and if the key is still pressed keep calling the method after a set time. The update() method is called too many times for my needs but I do not want to have the user keep pressing the same key to move in the same direction. The behaviour I'm after is similar to roguelike game movement if that clarifies things.
I'm currently doing this:
def button_down(id)
if button_down? Gosu::Button::KbLeft or button_down? Gosu::Button::GpLeft then
@foobar.move_left
end
end
I hope the above makes sense, any suggestions greatly appreciated.
Thanks.
what you are now doing is using the operating system's repeater, this can be different on other systems, or simply because of another users settings.
I'd create a variable holding a timestamp of the last call of the function, and if the current time is bigger than "timestamp + 100" (100 milliseconds later), I'd call the function again and reset timestamp to the current time. This will also save you using button_down(id) a lot (which is quite unusable in my opinion).
You might also need to check if the millisecond counter of gosu had an overflow (so if milliseconds is a really low value, and timestamp a very big one, that's probable)
If you want to check if anyone pressed a button once, you should rather check in button_up(id) because that is called only once (or implement some complex behavior through button_down, update and button_up ;) )
Well, I just tested it with a minimal ruby program, linux still has the repeater in it (I also checked if I have the newest gosu version from the svn repos)
Loading...