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 } }
Esta es mi clase. He hecho un constructor que toma dos objetos. He usado la siguiente línea de código para crear un nuevo objeto de clase. Debe ser una matriz de objetos.
const projectiles = [new Projectile({ position: { x:300, y:300 }, velocity: { x:0, y:0 } })]
El problema es que cuando estoy imprimiendo el valor de this.position.x
y this.position.y
en la consola, se está imprimiendo sin definir. Quiero el valor que pasé al crear el objeto.
update(){ console.log(this); // position: {position: {...}, velocity: {...}}, velocity: undefined, ... this.draw() this.position.x += this.velocity.x this.position.y += this.velocity.y }
Está pasando un argumento solo a la position
del parámetro.
así que deberías cambiar cambiar así
const projectiles = [new Projectile( // position { x:300, y:300 }, // velocity { x:0, y:0 } )]