-1

Thanks for the response. I have solved my problem. I really did see that its a list of callback functions. After some work i managed to shoot by intervals, but the first shot was after 1 second. 1 - a problem - if I call the function in setInterval imidiatly and then set interval - shoots rapidly. 2 - I fixed the problem by making setTimeout to set a bool value hasShooted to false after 1 second and if that value is false i can shoot. In the function i do that i set it to true. 3 - I realized I need only that last function with set timeout and not setInterval at 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);

Daniel petrov
  • 875
  • 9
  • 14

1 Answers1

0

You are setting a new interval every time onGameLoop is executed and this.isShooting equals to true. Therefore when you use clearInterval, you are clearing only the last interval, not all of them.

I recommend you clearing the variable shootInterval after clearing interval (for example: shootInterval = null;) and in the first condition (if (this.isShooting)) check if shootInterval is not null.

Your code should look like 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);
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177