Creating an instance of a class, I got null value.
I made Class Projectile as below.
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;
}
This new method created a valid class with x and y properties not null.
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)}`);
});
But on this line, same new method created a class with x and y properties null.
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",
[p.x, p.y]
)
);
p.used = true;
console.log(`projectiles1 = ${JSON.stringify(projectiles)}`);
}
});
});
}
What is the difference of them? And how to fix them?
if (p.time <= time1 && p.used === false) {
projectiles.push(
new Projectile(
p.time,
canvas.width / 2,
canvas.height / 2,
5,
"white",
[p.x, p.y]
)
);
[p.x, p.y] is not correct....
A velocity member is not an array, but dict.
Instead of [p.x, p.y], {x:p.x, y:p.y} solve this problem.