WDI-SEA / game-project-issues

0 stars 0 forks source link

generating random movement #43

Closed shuzel99 closed 3 years ago

shuzel99 commented 3 years ago

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

I'm trying to get my fruits at the top to fall in random directions

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

class Shopping{
    constructor(x, y, color, width, height, speed, direction){
        this.x = x //= Math.floor(Math.random() * game.width) generates random starting position
        this.y = y //= Math.floor(Math.random() * game.height)
        this.color = color
        this.height = height
        this.width = width
        this.alive = true
        this.speed = speed = Math.random() * (0.30 - 0.18) + 0.18
        this.direction = direction = Math.random() < 0.5 ? -1 : 1
    }
    render = function(){
        if (this.alive === true){
            ctx.fillStyle = this.color
        ctx.fillRect(this.x, this.y, this.width, this.height, this.speed, this.direction)
        }
         return
    }

}

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

Theres no error message but none of the items are moving at all.

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

Maybe the speed and direction aren't being called in the right place or in the right way.

What things have you already tried to solve the problem?

Tried putting the math.random on the x and y themselves but that just puts the items at a random spot with no movement

DoireannJane commented 3 years ago

Remember, Math. random will essentially give you a random number between 0 and 1