Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Keyboard input
- - By SPK Date 2011-10-24 12:18
Hey,

currently I'm working on a menu, which works, except that the
button_down?(key) method is called too often during the update interval.
That means if the KbUp button is pressed the index doesn't move one time, it moves
3 - 4 times.

I tried it with button_down instead of button_down?, but it ignores all input.
How can I avoid this problem?
Parent - By lol_o2 Date 2011-10-24 13:18
button_down?(key) is called one time, but each frame. If you want to make with this you can use a variable toggled by button.

if button_down?(KbDown) and !@pressed
  @index+=1
  @pressed=true
elsif not button_down?(KbDown)
  @pressed=nil
end


The second method uses a local variable as a button value.

def button_down(id)
  if id==KbDown then @index+=1 end
end
Parent - - By jlnr (dev) Date 2011-10-24 15:53
button_down is a method that you write and wait for Gosu to call it whenever a button is pushed down.
button_down? is a method that you call to ask Gosu whether a button is being held down right now. Gosu never calls button_down?.

By the way these methods work, button_down is what you use for one-time button presses, and button_down? is what you use to make buttons influence code in update - for example, walking left and right is a case for button_down? because you don't want the player to move only one pixel when he first hits the button, but you want them to move in every update tick.

class MyWindow ...
  def button_down(id)
    if id == Gosu::KbDown then
      move_down_in_menu...
    end
  end
Parent - - By SPK Date 2011-10-24 17:29
Thanks for your replies.

This :
def update
if button_down?(KbDown) and !@pressed
  @index+=1
  @pressed=true
elsif not button_down?(KbDown)
  @pressed=nil
end
end

and then I call @menu.update in my gosu::window update method and everything works. Great!

But I still don't understand when to use button_down(id)
When I replace my update method with button_down(id) and adjust it to the example above,
it isn't working.
As far as I understood Gosu calls button_down without any doing from my side, or?

If I put this inside my menu instead of the update snippet from above:

def button_down(id)
if id == Gosu::KbDown and !@pressed
  @index+=1
  @pressed=true
elsif not id == Gosu::KbDown
  @pressed=nil
end
end

Nothing happens. Where do I have to put this method for Gosu to call it?
Leaving it alone in my menu doesn't seem to work.
But since button_down? works I don't mind.
Parent - - By lol_o2 Date 2011-10-24 17:56
I gave you example of button_down(id)  (have you noticed it?).
It's like button_down?, but it's not repeated and is better for menu (because using it is shorter)
Parent - - By SPK Date 2011-10-24 18:01
I tried it, but nothing happens.
Where does id come from by the way?
Parent - - By lol_o2 Date 2011-10-24 18:28
It's a method argument    button_down( id )
It should work. Have you placed it in Window (along with update and draw)?

class YourWindow < Gosu::Window
  def update
  end

  def draw
  end

  def button_down(id)
  end
end
Parent - By SPK Date 2011-10-24 18:59
Ah, ok. I put it in my menu class.
So that's the flaw. Now it works.
Again, big thanks.
Parent - - By PhusionDev Date 2012-03-07 14:36
is there a way to abstract input out of the GameWindow << Gosu::Window
I have GameWindow calling update on my GameManager, which then has a check_input method. Is it possible to use button_down(id) this way?
Parent - - By jlnr (dev) Date 2012-03-07 15:26
Sure, Window can just call button_down(id) on the GameManager too. It's just a Ruby method call, no magic :)
Parent - By PhusionDev Date 2012-03-08 20:30
Thanks for this :)
Up Topic Gosu / Gosu Exchange / Keyboard input

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill