#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Updated game explosion


    As you can see by shooting down an enemy the explosion goes on forever and is very flickery, how can I get around this? "I managed to get the explosions working some bit"

    http://www.taffatech.com/DarkOrbit.html

    Also I use the following code at the start for smooth animation, could I use this?

    Code:
     var requestAnimFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||window.msRequestAnimationFrame  ||  window.oRequestAnimationFrame  ;
    On to the updated explosion code:


    function Explosion() //space weapon uses this
    {
    this.srcX = 0;
    this.srcY = 1250;
    this.drawX = 0;
    this.drawY = 0;
    this.width = 70;
    this.height = 70;
    this.currentFrame = 0;
    this.totalFrames = 10;
    this.ha**** = false;
    this.frame = 0;

    }

    Explosion.prototype.draw = function()
    {

    if(this.ha**** == true)
    {

    this.t=setInterval(this.Loop.bind(this), 500);

    }





    }

    Explosion.prototype.Loop = function()
    {
    this.frame++;
    ctxExplosion.clearRect ( 0 , 0, canvasWidth , canvasHeight );



    if(this.frame == 1)
    {

    ctxExplosion.drawImage(spriteImage,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY, this.width,this.height);

    }

    else if(this.frame == 2)
    {

    ctxExplosion.drawImage(spriteImage,(this.srcX + 77),this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height);

    }

    else if(this.frame == 3)
    {
    ctxExplosion.drawImage(spriteImage,(this.srcX + 154),this.srcY ,this.width,this.height,this.drawX,this.drawY,this.width,this.height);

    }

    else if(this.frame == 4)
    {
    ctxExplosion.drawImage(spriteImage,(this.srcX + 231),this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height);
    this.ha**** = false;
    this.frame = 0;
    clearInterval(this.t);

    }



    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0
    The game is running about 60fps. So is the function Explosion.prototype.draw, which means is has initiated about 33 timers before this.ha**** is set to false.
    Since this.t is only refering to last one, you'll have about 32 timers running forever.
    So a solution would be, to set this.ha**** to false directly after setting the first timer.

    Code:
    Explosion.prototype.draw = function(){
     if(this.ha**** == true){
      this.t = setInterval(Explosion.prototype.Loop.bind(this, counter++), 100);
      this.ha**** = false;
     }
    };
    
    Explosion.prototype.Loop = function(counter){
     ctxExplosion.clearRect(0, 0, canvasWidth, canvasHeight);
     if(this.frame < 4){
      ctxExplosion.drawImage(spriteImage, (this.srcX + 77 * this.frame), this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);
     }  
     else {
      this.frame = 0;
      this.t = clearInterval(this.t);
      ctxExplosion.clearRect(0, 0, canvasWidth, canvasHeight);
     }
     this.frame++;
    };

IMN logo majestic logo threadwatch logo seochat tools logo