I've tried to create a simple start menu for my game which informs the player to press a key to start the game but the problem is that it works only if the key is pressed all the time...i'm not sure what would be the right way to deal with this. Here is my code:
if (Game.keysPressed[KeyBindings.S]) {
if (game.livesLeft != 0) {
game.createEnemies();
game.update();
game.draw();
requestAnimationFrame(game.gameLoop);
}
if (game.livesLeft == 0) {
game.drawLostGame();
}
}
Basically if the S key is pressed the game works, but when you release the key Game.keysPressed[KeyBindings.S] becomes false and the if statement won't be executed anymore, so that's why i need to find a way that by pressing only once the if statement will be executed until the player loses. I've tried with while(true) and similar things but it didn't work. Help would be appreciated! Thanks
update
anddraw
would go inside yourgameLoop
- can you show us what thatgameLoop
method looks like? It appears as though you want to pass control from a start screen loop to a separate game loop, but instead you're continuing to loop the start screen. You shouldn't need to continue running the start screen's logic every frame once you've transitioned to the playing state. \$\endgroup\$