Delay when moving using keys

edited August 2017 in Questions about Code

Look, i coppied from Daniel this code:

void keyPressed() {
  if (keyCode == UP) { 
    heart.move(0, -1);
  } else if (keyCode == DOWN) { 
    heart.move(0, 1);
  } else if (keyCode == RIGHT) { 
    heart.move(1, 0);
  } else if (keyCode == LEFT) { 
    heart.move(-1, 0);
  }
}

It works with a thing i dont wanna happen, and is this: https://gyazo.com/7d306541e718332694de52a9cfb4d51e

If u see, it mvoes once, and latter it moves continously (cuz i am holding the key). But i only press and hold it, not press- release-press-hold.

Is there anyway u know to remove that small amount of delay?

Tagged:

Answers

  • This is due to Windows having a delay when holding the same key down. If you search "Keyboard Settings" on your computer you can navigate to this panel where you can minimize it. GoToLoop's example is the best though, using a different method to get input :)

  • okey. now i "fixed" it more, but still bad. I wanna make a mix between keyPressed() and between this new method.

    void fastMove() {
      if (keyPressed) {
        if (keyCode == UP) { 
          heart.move(0, -1);
        } else if (keyCode == DOWN) { 
          heart.move(0, 1);
        } else if (keyCode == RIGHT) { 
          heart.move(1, 0);
        } else if (keyCode == LEFT) { 
          heart.move(-1, 0);
        }
      }
    }
    

    with this method this is what i get: https://gyazo.com/ed01494f205619bbd3649c9c100d4c8d

    I can't stay on the mid row due to the speed of the update But there is not delay on moving also.

    So my plan was to use keyPressed() for up and down, and fastMove() for Right and Left.

    Can you help me?

  • For left-right movement, the method that GoToLoop shared is definitely the best way to go.

    For up-down if you want standard keyPressed() -- or perhaps even keyReleased(), which means you have to tap to move up and down rather than holding -- just use the normal function.

  • if i do fastMove() only for left & right and i put it inside draw, and i do keyPressed() for up and down, it doesnt work. when i press go left, it keeps moving for even. Even if i release the key

Sign In or Register to comment.