Al crear una instancia de una clase, obtuve un valor nulo.
Hice Class Projectile como se muestra a continuación.
class Projectile { constructor(time, x, y, radius, color, velocity) { this.time = time; this.x = x; this.y = y; this.radius = radius; this.color = color; this.velocity = velocity; } Este new método creó una clase válida con propiedades x e y no nulas.
addEventListener("click", (event) => { const angle = Math.atan2( event.offsetY - canvas.height / 2, event.offsetX - canvas.width / 2 ); const velocity = { x: Math.cos(angle) * 5, y: Math.sin(angle) * 5 }; var time1 = (new Date() - time) / 1000; if (gameState === true) answer.push(JSON.stringify(new Ans(time1, velocity.x, velocity.y))); projectiles.push( new Projectile( time1, canvas.width / 2, canvas.height / 2, 5, "white", velocity ) ); console.log(`projectiles = ${JSON.stringify(projectiles)}`); }); Pero en esta línea, el mismo método new creó una clase con propiedades x e y nulas. https://github.com/ekusiadadus/shootgame/blob/fb93a96c5eb071dad68b57da9fb4c87e7c572baa/index.html#L380
function checkBullet() { var timer = setInterval(() => { if (gameState === false) clearInterval(timer); let time1 = (new Date() - time) / 1000; submittion.forEach((p, i) => { if (p.time <= time1 && p.used === false) { projectiles.push( new Projectile( p.time, canvas.width / 2, canvas.height / 2, 5, "white", [px, py] ) ); p.used = true; console.log(`projectiles1 = ${JSON.stringify(projectiles)}`); } }); }); }¿Cuál es la diferencia de ellos? ¿Y cómo solucionarlos?
if (p.time <= time1 && p.used === false) { projectiles.push( new Projectile( p.time, canvas.width / 2, canvas.height / 2, 5, "white", [px, py] ) ); [px, py] no es correcto....
Un miembro de velocidad no es una matriz, sino un dict.
En lugar de [px, py] , {x:px, y:py} resuelve este problema.