Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!