WDI-SEA / game-project-issues

0 stars 0 forks source link

Issues with scope #51

Closed hannahlegros closed 3 years ago

hannahlegros commented 3 years ago

What's the problem you're trying to solve?

I'm trying to make my stop game function work, but I'm having issues with scope.

Post any code you think might be relevant (one fenced block per file)

const initiateGame = () =>{
    startButton.style.visibility = "hidden"
    explainGame.style.visibility = "hidden"
    messageBoard.innerText = "Avoid the humans!"
    alien.energy = 50
    alien.alive = true
    gamePlay = true
    setOrClearInterval()
    // human.alive = true
    // humanTwo.alive = true
    // booster.alive = true
    // highBooster.alive = true
    // console.log('this is the human:\n', human)
}
// clear game
// restart button
const stopGame = () => {
    ctx.clearRect(0, 0, game.width, game.height)
    startButton.style.visibility = "visible"
    explainGame.style.visibility = "visible"
    explainGame.innerText = "Another round?"
    gamePlay = false
    setOrClearInterval()
    // booster.alive = false
    // human.alive = false
    // console.log("human alive?\n", human)
}

function setOrClearInterval () {
    if (gamePlay == true){
        const intervalForGame = setInterval(playGame, 50)
        const spawnHumanInt = setInterval(spawnHuman, 2000)
        const spawnHumanTwoInt = setInterval(spawnHumanTwo, 3740)
        const spawnBoosterInt = setInterval(spawnBooster, 1620)
        const spawnHighBoostInt = setInterval(spawnHighBooster, 2770)
    } 
    if (gamePlay == false){
        clearInterval(intervalForGame)
        clearInterval(spawnHumanInt)
        clearInterval(spawnHumanTwoInt)
        clearInterval(spawnBoosterInt)
        clearInterval(spawnHighBoostInt)
    }
}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

script.js:315 Uncaught ReferenceError: intervalForGame is not defined at setOrClearInterval (script.js:315) at stopGame (script.js:300) at winOrLose (script.js:274) at playGame (script.js:260)

What is your best guess as to the source of the problem?

I need to move things around to change the scope for my intervalForGame.

What things have you already tried to solve the problem?

I've tried moving/ separating/ combining different functions and vars, but no success :( Either the game doesn't work as intended or I get another error message.