WDI-SEA / game-project-issues

0 stars 0 forks source link

getting objects to come into canvas randomly #10

Closed anaismveras closed 3 years ago

anaismveras commented 3 years ago

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

I have a movement function for my trash and i want it to move but I cant seem to figure out how to get more trash to show up on the screen to move the same way

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

function trashRender (x, y, color, width, height) {
    this.x = x
    this.y = y
    this.height = height
    this.color = color
    this.width = width
    this.alive = true
    // this.moveX = 4
    this.render = function() {
        ctx.fillStyle = this.color
        ctx.fillRect(this.x, this.y, this.width, this.height)
    }
}

let garbage = new trashRender(-1, 10, 'red', 10, 20)

// Trash movement function
const trashMovement = () => {
    if (garbage.alive) {
        garbage.x += 3
        garbage.y += Math.floor(Math.random() * 5)
    }
    if (garbage.alive && garbage.x + garbage.width >= ctx.width) {
        garbage.x = 0
    }
}

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

i dont see an error message

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

i think i need an array and a for loop somewhere

What things have you already tried to solve the problem?

Ive tried adding the trash into an empty array but i dont know how to get it to stop pushing, since my game is on a setInterval

tkolsrud commented 3 years ago

Are you having trouble making more trash appear on screen? or getting the trash that's on screen to move in the desired way?

anaismveras commented 3 years ago

getting more trash to appear on the screen

tkolsrud commented 3 years ago

So currently you've just got the one let garbage = new trashRender, if you wanted to create more trash at the outside you'd want to have to either call that method a few more times, or use a setInterval to have it happen automatically at time intervals

tkolsrud commented 3 years ago

create more trash at the outset*

anaismveras commented 3 years ago

Ive tried both of those methods already and they dont work meaning, either my trash just goes away or Idk how I would call the garbage variable if its inside a function its not in the gobal scoop anymore

tkolsrud commented 3 years ago

ah ha, lets check it out 1:1

anaismveras commented 3 years ago

okay

tkolsrud commented 3 years ago

Hey ana, were you ever able to get this resolved?