Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Help me with
- - By h4y4shi Date 2012-02-14 13:15
http://pastie.org/3380623

The above link is my attempt at modifying the tutorial.rb program. I am stuck right now.

What I want to do is this and then have the program wait for me to press the key
  def move
    @x = @x + 30
    @y = @y
  end

if button_down? Gosu::KbRight then
  @player.move
end

What the program does is it keeps changing the x +30 until I let go of the key. I want to be able to hold the right key down as long as I want and the most the x value will increase is +30, until I release the right key and press it again do I want the x value to be changed again. Please give me an example of this being done.
Parent - By lol_o2 Date 2012-02-14 13:45
Use button_down(id) method (place it in GameWindow class)

def button_down(id)
  if id==KbRight then @player.move end
end
Parent - By jlnr (dev) Date 2012-02-14 16:24
If something has no start, you can use button_down? alone.

If something happens once when you press a button, you use the button_down(id) callback as lol_o2 described. The tutorial uses this for the escape key.

So if you want to move the player once by 30 frames, instantaneously, you copy lol_o2's code. If you want the player to move over the course of several frames, but no more than 30 pixels, you need a combined solution. You could set a new variable to 30 in the button_down(id) callback. Then in update, when the button is down? you would see if the variable is still bigger than 30, move a bit and subtract the pixels. Otherwise, you wouldn't move (30 pixels already used up).
Parent - By h4y4shi Date 2012-02-14 19:31
Thank you. I managed to get it to work. Since I havent animated anything right now I am going to stick with the instant teleport hero 30 pixels once. Thanks for the help.
Up Topic Gosu / Gosu Exchange / Help me with

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill