WDI-SEA / game-project-issues

0 stars 0 forks source link

Game class crashing computer #37

Closed shuzel99 closed 3 years ago

shuzel99 commented 3 years ago

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

While the game is running it's constantly checking if a certain class has gotten into a collision and I think it's causing my computer to crash.

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

class Shopping{
    constructor(x, y, color, width, height){
        this.x = x
        this.y = y
        this.color = color
        this.height = height
        this.width = width
        this.alive = true
    }
    render = function(){
        if (this.alive === true){
            ctx.fillStyle = this.color
        ctx.fillRect(this.x, this.y, this.width, this.height)
        }
         console.log("Are u ok?")
    }
}

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

"Are u ok?" will log in the console several times per second until eventually my computer shuts down.

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

I think I need to limit the number of times the function checks if the items in the Shopping class are alive but I'm not sure how.

What things have you already tried to solve the problem?

Tried integrating setInterval but I might be doing it wrong.

tkolsrud commented 3 years ago

Yes, you're on the right track there! Setting the conditional to a boolean will continue to call it over and over again while this.alive remains true. Maybe refactor using the opposite logic, so that instead something happens when this.alive becomes false