class Projectile{
constructor(position, velocity){
this.position= position
this.velocity = velocity
this.radius = 30
}
draw(){
c.beginPath()
console.log(this.position.x + " " + this.position.y)
c.arc(500, 500, this.radius, 0, Math.PI * 2)
c.fillStyle = "red"
c.fill()
c.closePath()
}
update(){
this.draw()
this.position.x += this.velocity.x
this.position.y += this.velocity.y
}
}
This is my class. I have made a constructor that takes two objects. I have used following line of code to make a new object of class. It should be an array of objects.
const projectiles = [new Projectile({
position: {
x:300,
y:300
},
velocity: {
x:0,
y:0
}
})]
The problem is when I am printing the value of this.position.x and this.position.y in console , it is printing undefined. I want the value that I have passed while creating the object.
update(){
console.log(this); // position: {position: {...}, velocity: {...}}, velocity: undefined, ...
this.draw()
this.position.x += this.velocity.x
this.position.y += this.velocity.y
}
You are passing an argument only to the parameter position.
so you should change change like this
const projectiles = [new Projectile(
// position
{
x:300,
y:300
},
// velocity
{
x:0,
y:0
}
)]