0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Before answering, I should say the name of your topic is "low quality" and will not attract the right kind of answers. You should specify something about a start menu. I'll see if I'm able to come up with an answer below. \$\endgroup\$ – blurry Jun 3 '18 at 18:59
  • \$\begingroup\$ Ordinarily things like update and draw would go inside your gameLoop - can you show us what that gameLoop 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\$ – DMGregory Jun 3 '18 at 20:21
0
\$\begingroup\$

You should be setting a bool and then checking that. Something like this should fix it:

// in the initialization code
bool gameNotStarted = true;

// all that stuff in between
// [...]

// check if it's time to start the game loop!
if(gameNotStarted)
{
    if (Game.keysPressed[KeyBindings.S] && gameNotStarted) {
        gameNotStarted = false;
    }
}

// specifically not an else statement because it would cause a delay
if(gameNotStarted == false)
{
    if (game.livesLeft != 0) {
        game.createEnemies();
        game.update();
        game.draw();
        requestAnimationFrame(game.gameLoop);
    }
    if (game.livesLeft == 0) {
        game.drawLostGame();
    }
}

Your current issue is that each time you want to animate you also check the keyboard; meaning the game will only run while you have the key down. Simply "refactoring" the logic above should fix this issue.

\$\endgroup\$
0
\$\begingroup\$

Thanks for answering! In the end i made it work doing something like this:

gameLoop() {

    if (game.startGame == 0) {
        game.update();
        game.drawBoardDetails();
        game.drawStartScreen();
        game.startGame = 1;
    }else if (Game.keysPressed[KeyBindings.S]) {
        if (game.livesLeft != 0) {
            game.backgroundAudio();
            game.createEnemies();
            game.update();
            game.draw();
        }
        if (game.livesLeft == 0) {
            game.drawLostGame();
        }
    }else if (Game.keysPressed[KeyBindings.S] == false) {
        Game.keysPressed[KeyBindings.S] = true;
    }
     requestAnimationFrame(game.gameLoop);

}

The code is kind of weird but it works fine:))

\$\endgroup\$
  • \$\begingroup\$ That's the basis of having multiple modes, now you can easily add a pause screen for example. Or make that start screen have a options menu. \$\endgroup\$ – ratchet freak Jun 4 '18 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.