r/processing • u/marvelcomics22 • Feb 11 '25
Help request How can I restart my game within the code?
Hi! I'm making a game and one aspect of it is that you need to do the thing before a creature catches up to you, and if it does catch up to you, then it shows a png that says click space to restart, so I wrote:
if (keyPressed && key == ' ') {
setup ();
}
It goes to the page, but then immediately flicks back to the png telling them to click space to restart. Please advise and thank you!
2
Upvotes
7
u/Simplyfire Feb 11 '25
write your own resetGame() function that you call both from setup() and from the keyPressed event
4
u/IJustAteABaguette Seeker of Knowledge Feb 11 '25
You can't really do this.
A better way is to set every value to the original value.
Set the X and Y of the player back to the spawnpoint, change the game state to the original, those sorts of things.
2
u/scratchisthebest Feb 11 '25
Btw, the reason this doesn't work in Processing is because you can't call
sizemore than once:and in p5.js, you can't call
createCanvasmore than once: