My student wants the code if (keyDown(“space”)) {
game();
} to stay at the game but when you let up on the space key it goes back to the original.
To fix this issue, you must create a global variable with any name you want. This will store the data that the spacebar has been held down.
var screen = "Start"
When the key is down, you should change the value of the variable to anything you want, but for this example, I will therefore use “Game”.
if (keyDown("space")) {
screen = "Game";
}
The function will be called when it checks the value with an equality operator, then you can call the function in.
if (screen == "Game") {
game();
}
I hope your student does well!
If you didn’t understand what I just did, here is a remix when it’s fixed.
I have it marked with comments blocks so that you can see what I have changed.
This is a good solution, but a simpler one would be to just change it from keyDown to keyWentDown. Remember, keyDown is only while the key indicated is actually being held down.
Yes I know, but I wasn’t sure if I should change something that wasn’t the actual problem you mentioned.