0
\$\begingroup\$

I'm creating a space shooter using type script and I don't understand why explosions won't work meaning that nothing is drawn on the screen and I don't understand what I'm doing wrong.

for (var i = 0; i < projectileList.length; i++) {
    if (this.y >= projectileList[i].y) {
        let projDist = this.x - projectileList[i].x;
        if (projDist < 0) {
            projDist = projDist * (-1);
        }

        if (projDist >= 0 && projDist <= 30) {
            this.x = Math.floor(Math.random() * 600) + 1;
            this.y = 20;
            projectileList[i].x = 2000;
            for (var j = 1; j <= 9; j++) {
                this.sprite = "/images/explode_" + j + ".png";
                this.img.src = this.sprite;
                this.gameContext.drawImage(this.img, 200, 200);
            }
        }
    }
}

Basically when there is a collision between projectiles and asteroids, I'm just trying to draw some images on the screen but nothing is happening and the console won't give any errors :( Do you guys know what is wrong?

Thanks for help!

\$\endgroup\$
  • \$\begingroup\$ For loops are instant. So you'd be drawing 9 sprites for only 1 frame. Is that the intend? \$\endgroup\$ – Sidar May 30 '18 at 15:42
  • \$\begingroup\$ No, i don't want that \$\endgroup\$ – Player May 30 '18 at 15:47
  • \$\begingroup\$ Have you considered moving the "sprites" to individual objects that have more control over time/discarding. Or a particle system? \$\endgroup\$ – Sidar May 30 '18 at 15:49
  • \$\begingroup\$ I'm not sure what do you mean with moving the sprites to individual objects? \$\endgroup\$ – Player May 30 '18 at 15:50
  • \$\begingroup\$ Your mistake was to assume that the for loop would show the explosion over several frames. A for loop is executed in place from where it's called. So if you want the explosion to show up, animate and then be removed you need a way to control the state of the explosion. You could write a little system that handles the explosion ( often times a Particle System would fit) or you create a class that handles the animation and graphics calls by itself and spawn that. \$\endgroup\$ – Sidar May 30 '18 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.