Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
JavaScript's new operator makes variables Null

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.

https://github.com/ekusiadadus/shootgame/blob/fb93a96c5eb071dad68b57da9fb4c87e7c572baa/index.html#L361

    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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

          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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda