'KeyIsDown does not seem to work with the right arrow

I'm writing a simple 2D platformer game on p5js, with Visual Studio Code 2022. I want to move this dinosaur sprite to the right and the left, however the keyIsDown for the right arrow doesn't work. I get no error message, and the game is always functional, and the key on the keyboard is working, but the sprite doesn't move to the right. Any ideas on how to solve this?

the code I wrote: code image



Solution 1:[1]

Two answers:

  1. You could use the function keyIsPressed(LEFT_ARROW)

  2. You could have a separate function keyIsDown() outside of the draw function.

function keyIsDown(){

  if(key == LEFT_ARROW){

    doTheDinoStuff();
    
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KoderM