Page 1 of 1

Keyboard events help: Frameskips

Posted: Thu Jan 19, 2017 4:20 pm
by ManaSV
Hello Guys,
I need some help solving a problem we have with some friends I am developing (prototyping) a game with.
Basically my task at the moment is basic movement to the player (at the time Im researching about input methods), what I have now is a 3x3 grid that is the board where the player is supposed to be moving in, the grid is like this, I draw the player at [0,0]

[-1,-1][0,-1][1,-1]
[-1, 0 ][ 0, 0][ 1, 0]
[-1, 1 ][ 0, 1][ 1, 1]

When the player press (up,down,right or left) it is supposed to move 1 time in that direction, but what I got is that I got some frameskips if I can name it like that, because I cant move with accuracy where I want, basically Im moving in the corners because I cant only get my keyboard to register one tap to the key, instead I got like I keep pressing down the key (I think is because im using isDown) tried with keypressed but no luck implementing that..

Here is my code, hope you can help, thanks in advance!

Code: Select all

function Player:update(GridWidth,GridHeight)
  if love.keyboard.isDown("right") then
    if self.xStatus == 0 or self.xStatus == -1  then
      self.x = self.x + GridWidth
      self.xStatus = self.xStatus + 1
    end
  elseif love.keyboard.isDown("left") then
    if self.xStatus == 0 or self.xStatus == 1  then
      self.x = self.x - GridWidth
      self.xStatus = self.xStatus - 1
    end
  end
  if love.keyboard.isDown("up") then
    if self.yStatus == 0 or self.yStatus == 1  then
      self.y = self.y - GridHeight
      self.yStatus = self.yStatus - 1
    end
  elseif love.keyboard.isDown("down") then
    if self.yStatus == 0 or self.yStatus == -1  then
      self.y = self.y + GridHeight
      self.yStatus = self.yStatus + 1
    end
  end
end

Re: Keyboard events help: Frameskips

Posted: Thu Jan 19, 2017 7:06 pm
by raidho36
Well yeah, isDown will continuously report "true" for as long as key is pressed, and you will move the player every update function (may be triggered well over 100 times per second) for as long as key is pressed. If you want to detect the moment it goes from true to false and vice versa, you need to keep the state from previous call and compare it to current state - if it's not the same then there was a change.

There are keypressed and keyreleased events that fire exactly once when a key is pressed or released.

Re: Keyboard events help: Frameskips

Posted: Thu Jan 19, 2017 9:48 pm
by ManaSV
Kind of get it working now

Some of my main.lua:

Code: Select all

function love.update(dt)
    player:update(grid.width,grid.heigth)
end

function love.draw()
    love.graphics.print(love.timer.getFPS())
    grid:draw()
    player:draw()
end
Some of my player.lua

Code: Select all

function Player:draw()
  if (not self.keyDown) then
    love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
  end
end

function Player:update(GridWidth,GridHeight)
  function love.keypressed(key)
    if key == "right" then
      if self.xStatus == 0 or self.xStatus == -1  then
        self.x = self.x + GridWidth
        self.xStatus = self.xStatus + 1
      end
    elseif key == "left" then
      if self.xStatus == 0 or self.xStatus == 1  then
        self.x = self.x - GridWidth
        self.xStatus = self.xStatus - 1
      end
    end

    if key == "up" then
      if self.yStatus == 0 or self.yStatus == 1  then
        self.y = self.y - GridHeight
        self.yStatus = self.yStatus - 1
      end
    elseif key == "down" then
      if self.yStatus == 0 or self.yStatus == -1  then
        self.y = self.y + GridHeight
        self.yStatus = self.yStatus + 1
      end
    end

    self.keyDown = true
  end

  function love.keyreleased(key)
    if (key == "right" or key == "left" or key == "down" or key == "up") then
      self.keyDown = false
    end
  end

end
Just don't know if Im doing in the keyboard management in an optimized way, as for now my new bug is that if I leave pressed the key, it just move one position (like I want it to be) but I only paint the player when the keys are not pressed, I know that I have to work more on the logic, but I want know at least if from the last code, I improved or I'm going in the wrong way, thanks!

EDIT: lmao just noticed that the last change I made (keyDown flag) isnt necesary, just keypressed did the trick.. removed it because it was causing that the player wasnt drawing when the key was pressed.

Re: Keyboard events help: Frameskips

Posted: Fri Jan 20, 2017 12:43 am
by raidho36
You have in there condition that checks if any keys are pressed, before drawing. If you remove that check, it will draw unconditionally.

Re: Keyboard events help: Frameskips

Posted: Fri Jan 20, 2017 12:59 am
by ManaSV
raidho36 wrote:You have in there condition that checks if any keys are pressed, before drawing. If you remove that check, it will draw unconditionally.
Thank you , I was editing my last message when you answered again haha, I think you refer to keyPressed flag, I figured it out a moment ago and removed it, now it draws like expected (I think :P).