Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Limiting speed of user input / player movement
- - By bleakcabal Date 2012-07-07 03:33
Hi,

I started developing a small 2D tile game which uses a first person 2D view. The view and movement is along the lines of Dungeon Master, Eye of the Beholder, Bard's Tale, Might and Magic, etc.

My current problem is that when I press a key to move. The game moves way too fast. I know that the game is running in a loop and that it must draw a certain number of frames per second. Considering these facts, what is the best, cleanest way to limit the speed of player movement / user input ?

Ideally, I would like that if the user holds the foward arrow key, the game moves one square, has a small delay and then moves again.
Parent - - By jlnr (dev) Date 2012-07-07 04:43
There are two mechanisms for handling input. One is checking for button_down?(id) in Window#update. This will happen 60 times per second.

The other is reacting to the Window#button_down callback. This is how the Tutorial game handles the Escape key. This is called only once for the whole duration of holding the button.

I'm sorry for the confusing names here. :)

You would use a combination of both to implement what you need. Maybe like this:

TICKS_PER_STEP = 30

def initialize
  ..
  @countdown = 0
end

def update
  ..
  if @countdown > 0 then
    @countdown -= 1
    # Second, third… step
    if @countdown == 0 and button_down? Gosu::KbLeft then
      @countdown = TICKS_PER_STEP
      move_left
    end
  end
  ..

def button_down(id)
  if id == Gosu::KbLeft and @countdown == 0 then
    # First step
    @countdown = TICKS_PER_STEP
    move_left
  end
end


This also ensures that the player cannot move faster than one step per TICKS_PER_STEP even if they hammer the left key. If the speed of movement should be unrestricted for hammering keys, then you can just reset @countdown in the button_up(id) callback.
Parent - By bleakcabal Date 2012-07-12 02:05
Thanks !
Up Topic Gosu / Gosu Exchange / Limiting speed of user input / player movement

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill