I’m currently developing a simple 2D game in JavaScript using the p5.min library. The game features a character navigating through a landscape, where they must jump over a hole to collect an item. However, if the character falls into the hole, they’re meant to plummet without the ability to jump or move left or right.
To implement this mechanic, I’ve introduced a global boolean variable called “isPlummeting,” initialized to “false” at the start of the game. The purpose of “isPlummeting” is to restrict the character’s movement when they are over or inside the hole by setting it to “true.”
I update the value of “isPlummeting” within the “draw()” function and intend to use it in the event function “keyPressed()” to halt movement or jumping while the character is falling. However, I’ve encountered an issue where continuous key presses don’t seem to update the global variables within the “keyPressed()” function.
I’m wondering if my assumption is incorrect and if there’s a method to ensure “isPlummeting” updates its status while a key is held down continuously. Any insights or suggestions would be greatly appreciated.
The only solution I’ve found to address this issue is to directly utilize “isPlummeting” within the “draw()” function.
By incorporating “isPlummeting” directly into the “draw()” function, I’ve managed to ensure that the character’s actions appropriately respond to their falling state. This approach has helped maintain the game’s intended mechanics, allowing for accurate control over the character’s movements when encountering the hole.
ANTONIO ERNESTO FORLI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.