WDI-SEA / game-project-issues

0 stars 0 forks source link

creating multiple objects with the same properties #39

Closed lawrencesalinas closed 3 years ago

lawrencesalinas commented 3 years ago

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

shoot circles everytime I press space bar. creating new circles and fire them at will.

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

function Circle(x, y, radius){
    this.x = x
    this.y = y
    this.radius = radius
    this.draw = () =>{
        c.beginPath()
        c.arc(this.x , this.y, this.radius, 0, Math.PI * 2, false)
        c.stroke()
    }
}

let circle = new Circle(50, 0, 50,)

// let circleBox = []
// const createCircle = () => {
//     let circle = new Circle('circle',50, 0, 10, 0)
//     circleBox.push(circle)
//     console.log(circleBox)
//     for(let i = 0; i < circleBox.length; i++){
//         circle = circleBox[i]
//        }
// }

var controls = {}
const controller = (key, keyDown) => {
if(key == " "){
    controls.shooting = keyDown
    // createCircle()
}
}

const move = () => {
    if(controls.shooting){
        circle.x = 100 + player2.x
        circle.y = player2.y
        circle.y -= 10

    //     createCircle()
    //     for(let i = 0; i < circleBox.length; i++)
    //     circleBox[i].draw()
    }
}

const shootCircle = () => {
     circle.y -= 10
}

const clearInt = () =>{
    c.clearRect(0, 0, canvas.width, canvas.height)
}

const gameLoop = () => {

    shootCircle()
    clearInt()
    circle.draw()
    move()

}

gameInterval = setInterval(gameLoop, 1000/60)
document.addEventListener("keydown", (e) =>{
    controller(e.key, true)
})
document.addEventListener("keyup", (e) => {
    controller(e.key, false)
})

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

I wa trying to create for loops to create multiple circles. everytime I press the button to fire, the circle comes back to the origin point

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

I only have one circle

What things have you already tried to solve the problem?

tried to create an array of ciicle object witht the same properties using a for loop

timmshinbone commented 3 years ago

Ok, some of your code needs to be addressed, and then we can get to the issue at hand. First and foremost, we are NOT using var to define variables, we are using let for variables that can be reassigned values, and const for variables that cannot be reassigned.

Ok, on to the issue at hand, you are creating circles, and then pushing them into an array of circles, but you're not utilizing that array, which is necessary to add these circles to the canvas and display their movement. What do you think you can do with that array in order to show all of the circles in the array on the canvas?

lawrencesalinas commented 3 years ago

Understood. I am trying to use that created array in a function that will make it show the circles. I will post updates.

lawrencesalinas commented 3 years ago

I was able to make multiple circles at will at any point of time using the array . Issues is close.