I'm currently creating a final project that is supposed to be similar to the classic Scary Maze Game. I'm using Vanilla JS and HTML5 Canvas and would prefer not to use outside libraries or frameworks.
My game is coming along fine, I just have an issue that I cannot seem to debug. So, I want to create a function called redo() that I can insert between the if statements that check for my mouse position (lines 88-112 in the firstLevel() function and 161-233 in secondLevel() function). Right now, in place of the redo() function I put some code in that resets the mouse position to a certain point -- which works. But, when I place a function like return startUp() or return redo(), it doesn't work.
Why does return [insert function] not work in if statements, but plain lines of code do?
Does anyone have any advice on how I can attain my goal: when the user's mouse exits the blue region, it takes them back to the startup screen?
Link to my project: https://github.uconn.edu/pages/ssw19002/dmd-3475/final-project/maze-page-1.html
Code that checks mouse position (that works):
setInterval(()=> {
if(mouseX <= 159 || mouseX >= 870 ){
// Resets at the position:
mouseX = 257;
mouseY = 620;
}
if(mouseX <= 850 && mouseX >= 150)
{
if(mouseX >= 370 && mouseY >= 250){
mouseX = 257;
mouseY = 620;
}
}
if(mouseY <= 200 || mouseY >= 620){
mouseX = 257;
mouseY = 620;
}
// Red region
if(mouseX > 850 && mouseX < 870){
if(mouseY > 201 && mouseY < 220){
return secondLevel();
}
}
}, 500);
What I want code to look like, but is not working:
setInterval(()=> {
if(mouseX <= 159 || mouseX >= 870 ){
// Resets at the position:
return startUp();
}
if(mouseX <= 850 && mouseX >= 150)
{
if(mouseX >= 370 && mouseY >= 250){
return startUp(); // doesn't seem to work
}
}
if(mouseY <= 200 || mouseY >= 620){
return startUp(); // doesn't seem to work
}
// Red region
if(mouseX > 850 && mouseX < 870){
if(mouseY > 201 && mouseY < 220){
return secondLevel(); //this line of code works – it transitions to the next level
}
}
}, 500);
Aucun commentaire:
Enregistrer un commentaire