clearInterval不清除间隔

时间:2016-01-15 22:59:39

标签: javascript clearinterval

感谢您的回复。我已经解决了我的问题。我确实看到了它的回调函数列表。经过一些工作,我设法间隔拍摄,但第一次拍摄是在1秒后。 1 - 一个问题 - 如果我在imIniatly中调用setInterval中的函数然后设置interval - 快速拍摄。 2 - 我通过使setTimeout在1秒后将bool值hasShooted设置为false来解决问题,如果该值为false,我可以拍摄。在我这样做的功能中,我将其设置为true。 3 - 我意识到我只需要设置超时的最后一个函数,而不是setInterval。 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;

})(经理);

1 个答案:

答案 0 :(得分:0)

每次执行onGameLoopthis.isShooting等于true时,您都会设置新的时间间隔。因此,当您使用clearInterval时,您只清除最后一个间隔,而不是所有间隔。

我建议您在清除间隔后清除变量shootInterval(例如:shootInterval = null;),并在第一个条件(if (this.isShooting))中检查shootInterval是否为空。

您的代码应如下所示:

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);