Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / button_down
- 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.
- By oli.obk Date 2008-11-09 12:25
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 ;) )
- By salin Date 2008-11-09 14:20
Thanks for reply.  Yes that makes sense.  I'll use button_up to send a cancel message to the move method when it's running after having been called by button_down initially and set repeating per my time interval.
- By jlnr (dev) Date 2008-11-09 19:32

> 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.


The operating system's repeater actually shouldn't trigger button_down events, but I'm not sure if I stopped it from doing so on all operating systems already. *adds a To Do item to check that*
- By oli.obk Date 2008-11-10 14:13
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)
Up Topic Gosu / Gosu Exchange / button_down

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill