javascript - clearInterval not clearing the interval -


thanks response. have solved problem. did see list of callback functions. after work managed shoot intervals, first shot after 1 second. 1 - problem - if call function in setinterval imidiatly , set interval - shoots rapidly. 2 - fixed problem making settimeout set bool value hasshooted false after 1 second , if value false can shoot. in function set true. 3 - realized need last function set timeout , not setinterval @ all. var playermanager = (function(parent){ 'use strict';

var bulletpossleft,     bulletposstop,     fire_speed = 1000,     hasshot = false;  playermanager.prototype = object.create(parent.prototype);  function playermanager() {      parent.call(this);       this.moveleft= false;      this.moveright= false;      this.moveforward= false;      this.moveback= false;      this.isshooting= false;      this.bulletmanager = new bulletmanager(); }  playermanager.prototype.ongameloop = function(obj) {     if (this.isshooting) {         bulletpossleft = obj.positionleft + math.floor(obj.planewidth /2);         bulletposstop = obj.positiontop - math.ceil(obj.planeheight /2);          if(!hasshot){             this.shoot();             hasshot = true;             settimeout(function(){                 hasshot = false;             }, fire_speed);         }     }      if (this.moveleft && (obj.positionleft - obj.speed) > 0) {         obj.positionleft -= obj.speed;     }     if (this.moveright && (obj.positionleft + obj.speed) < game.getcontextvalue('width')) {         obj.positionleft += obj.speed;     }     if (this.moveforward && (obj.positiontop - obj.speed) > 0) {         obj.positiontop -= obj.speed;     }     if (this.moveback && (obj.positiontop + obj.speed) < game.getcontextvalue('height')) {         obj.positiontop += obj.speed;     }      obj.move(); };  playermanager.prototype.shoot = function(){     this.bulletmanager.spawn(new bullet(bulletpossleft, bulletposstop, 'orange')); };  playermanager.prototype.keyboardlistener  =  function(e) {      var value = e.type == 'keydown';      switch (e.keycode) {         case 37:             this.moveleft = value;             break;         case 38:             this.moveforward = value;             break;         case 39:             this.moveright = value;             break;         case 40:             this.moveback = value;             break;         case 32:             this.isshooting = value;             break;         default:             break;     } };  return playermanager; 

})(manager);

you setting new interval every time ongameloop executed , this.isshooting equals true. therefore when use clearinterval, clearing last interval, not of them.

i recommend clearing variable shootinterval after clearing interval (for example: shootinterval = null;) , in first condition (if (this.isshooting)) check if shootinterval not null.

your code should this:

var bulletpossleft,     bulletposstop,     firespeed = 1000,     shootinterval,     self;  playermanager.prototype = object.create(parent.prototype);  function playermanager() {      parent.call(this);       this.moveleft= false;      this.moveright= false;      this.moveforward= false;      this.moveback= false;      this.isshooting= false;      this.bulletmanager = new bulletmanager();       self = this; }  playermanager.prototype.ongameloop = function(obj) {     if (this.isshooting && shootinterval == null) {         bulletpossleft = obj.positionleft + math.floor(obj.planewidth /2);         bulletposstop = obj.positiontop - math.ceil(obj.planeheight /2);         shootinterval = setinterval(function(){             self.shoot();          } , firespeed);     }      if(!this.isshooting) {         clearinterval(shootinterval);         shootinterval = null;     }      if (this.moveleft && (obj.positionleft - obj.speed) > 0) {         obj.positionleft -= obj.speed;         debugger;      }     if (this.moveright && (obj.positionleft + obj.speed) < game.getcontextvalue('width')) {         obj.positionleft += obj.speed;     }     if (this.moveforward && (obj.positiontop - obj.speed) > 0) {         obj.positiontop -= obj.speed;     }     if (this.moveback && (obj.positiontop + obj.speed) < game.getcontextvalue('height')) {         obj.positiontop += obj.speed;     }      obj.move(); };  playermanager.prototype.shoot = function(){     this.bulletmanager.spawn(new bullet(bulletpossleft, bulletposstop, 'orange')); };  playermanager.prototype.keyboardlistener  =  function(e) {      var value = e.type == 'keydown';      switch (e.keycode) {         case 37:             this.moveleft = value;             break;         case 38:             this.moveforward = value;             break;         case 39:             this.moveright = value;             break;         case 40:             this.moveback = value;             break;         case 32:             this.isshooting = true;             break;         default:             break;     }      if(e.type == 'keyup'){         this.isshooting = false;     } };  return playermanager;  })(manager); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -