WDI-SEA / game-project-issues

0 stars 0 forks source link

Random object to move by itself #4

Closed LindZCoding closed 2 years ago

LindZCoding commented 2 years ago

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

Trying to get a random object to move

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

`let asteroidOne = new Crawler(0, 0, "pink", 60, 60)`
`let asteriods = []`

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

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

Somehow using setInterval and an array and maybe x and y coordinates

What things have you already tried to solve the problem?

Random things on google...

timmshinbone commented 2 years ago

ok, do you have a movement function built into your asteroid class?(looks like you're calling it Crawler, which wouldn't be accurate in this instance, remember that's just what we called our characters in the dungeon crawler demo) you might want to create a class for asteroids(bad guys) and a separate class for your spaceship(player), if you have a movement function, share that in a code block in a comment

LindZCoding commented 2 years ago

I only have my player in a movement function right now using the keys

let movementHandler = (e) => {
    switch(e.key.toLowerCase()) {
        case "a":
            //move left
            player.x -= 190
            if (player.x <= 0) {
                player.x = 0
            }
            break
        case "d":
            //move right
            player.x += 190
            if (player.x + player.width >= game.width) {
                player.x = game.width - player.width
            }
            break

    }

}

and I just made a sep class for player and meteor (changed all from asteroid to meteor)

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

function Player(x, y, color, width, height) {
    this.x = x
    this.y = y
    this.color = color
    this.width = width
    this.height = height
    this.alive = true
    this.render = function () {
        ctx.fillStyle = this.color
        ctx.fillRect(this.x, this.y, this.width, this.height)
    }
}  
LindZCoding commented 2 years ago

and right now I have the first meteor spawning in the top left corner

timmshinbone commented 2 years ago

So a meteor would need its own move function, that would move it from the point of origin towards the place you want the meteor to go. so, something like, meteor.x += 10 or something along those lines. How do you think a function like that would look?

LindZCoding commented 2 years ago
let meteorMovement = (e) => {
    meteorOne.x += 10
}

and then I called it in my game loop func and it is now moving by itself! Just gotta tweak it to how I want it now
timmshinbone commented 2 years ago

Would you say that this issue has been resolved? If so, go ahead and click 'close issue' at the bottom