javascript中函数的可访问性

时间:2018-05-16 17:48:12

标签: javascript inheritance

这是来自javascript的问题。

    if (game.ship.livesPool.getPool[0].getDamaged())

我在这个getDamaged()函数中得到一个错误,因为在另一个函数中未定义。

game = new Game();

游戏在此函数之外定义为全局变量。

this.ship = new Ship();

ship在Game类中定义。

this.livesPool = new Pool();
var pool = [];
this.getPool = function(){
  return pool;
}

livesPool在Pool类中定义。 pool是Pool类中定义的数组。 getPool函数将返回此数组。

pool[i] = new Lives();

每个pool [i]都将在Pool类中分配Lives对象。

this.getDamaged = function(){
    return this.damaged;
}

getDamaged()函数在Lives类中以这种方式定义。

为什么它告诉我这个函数是未定义的?

游戏课

function Game() {
this.init = function () {
    // Obtain the canvas from HTML
    this.bgCanvas = document.getElementById('background');
    this.shipCanvas = document.getElementById('ship');
    this.mainCanvas = document.getElementById('main');

    // Load the context
    /* Just one of them passing to conditional statement is enough to test
     * the availability
     */
    if (this.bgCanvas.getContext) {
        this.bgContext = this.bgCanvas.getContext('2d');
        this.shipContext = this.shipCanvas.getContext('2d');
        this.mainContext = this.mainCanvas.getContext('2d');

        Background.prototype.context = this.bgContext;
        Background.prototype.canvasHeight = this.bgCanvas.height;
        Background.prototype.canvasWidth = this.bgCanvas.width;
        Ship.prototype.context = this.shipContext;
        Ship.prototype.canvasHeight = this.shipCanvas.height;
        Ship.prototype.canvasWidth = this.shipCanvas.width;
        Lives.prototype.context = this.shipContext;
        Lives.prototype.canvasHeight = this.shipCanvas.height;
        Lives.prototype.canvasWidth = this.shipCanvas.width;
        Bullet.prototype.context = this.mainContext;
        Bullet.prototype.canvasHeight = this.mainCanvas.height;
        Bullet.prototype.canvasWidth = this.mainCanvas.width;
        Bullet.prototype.bossContext = this.shipContext;
        Enemy.prototype.context = this.mainContext;
        Enemy.prototype.canvasHeight = this.mainCanvas.height;
        Enemy.prototype.canvasWidth = this.mainCanvas.width;
        Boss.prototype.context = this.shipContext;
        Boss.prototype.canvasHeight = this.shipCanvas.height;
        Boss.prototype.canvasWidth = this.shipCanvas.width;

        // Define background in the game
        this.background = new Background();
        this.background.init(0, 0, imageRepository.background.width, imageRepository.background.height);

        // Define ship in the game
        this.ship = new Ship();
        var shipStartX = this.shipCanvas.width / 2 - imageRepository.ship.width / 2;
        var shipStartY = this.shipCanvas.height / 4 * 3 + imageRepository.ship.height / 2;
        this.ship.init(shipStartX, shipStartY, imageRepository.ship.width, imageRepository.ship.height);
        this.ship.type = "ship";
        this.ship.hasExplored = false;

        // Define enemy pools in the game
        this.enemyPool = new Pool(10);
        this.enemyPool.init("enemy");

        this.boss = new Boss();

        return true;
    }
    else {
        return false;
    }
};

this.runGame = function () {
    this.ship.draw();
    animate();
};

this.restart = function () {
    this.bgContext.clearRect(0, 0, this.bgCanvas.width, this.bgCanvas.height);
    this.mainContext.clearRect(0, 0, this.mainCanvas.width, this.mainCanvas.height);
    this.shipContext.clearRect(0, 0, this.shipCanvas.width, this.shipCanvas.height);

    this.enemyPool.init("enemy");

    var shipStartX = this.shipCanvas.width / 2 - imageRepository.ship.width / 2;
    var shipStartY = this.shipCanvas.height / 4 * 3 + imageRepository.ship.height / 2;
    this.ship.x = shipStartX;
    this.ship.y = shipStartY;
    this.ship.hasExplored = false;
    this.ship.bulletPool.init("bullet");

    score = 0;
    displayedScore = 0;

    bossExist = false;
    this.boss.life = 100;
    this.boss.bulletPool.init("boss_bullet");
    this.boss.fireRate = 0;

    while(this.ship.livesPool.getSize() < 3){
        this.ship.livesPool.increaseLives();
    }
    document.getElementById('game-over').style.display = "none";

    this.runGame();
};

this.start = function () {
    gameStarted = true;
    document.getElementById('main-menu').style.display = 'none';
    document.getElementById('score-board').style.display = 'block';
};

this.backHome = function () {
    gameStarted = false;
    document.getElementById('game-over').style.display = 'none';
    document.getElementById('score-board').style.display = 'none';
    document.getElementById('main-menu').style.display = 'block';
    this.restart();
};

}

船级。

function Ship() {
this.speed = 5;
this.bulletPool = new Pool(maxNumOfBullets);
this.bulletPool.init("bullet");
this.bulletSoundPool = new SoundPool(maxNumOfBullets);
this.bulletSoundPool.init("bullet");
this.livesPool = new Pool(3);
this.livesPool.init("lives");
this.hasExplored = false;
this.life = 3;

var fireRate = 15;
var counter = 0;

this.draw = function () {
    if (this.livesPool.getSize() <= 0) {
        this.context.clearRect(this.x, this.y, this.width, this.height);
    }
    else {
        this.context.drawImage(imageRepository.ship, this.x, this.y);
    }
}
this.move = function () {
    counter++;
    // Determine if the action is move action
    if (KEY_STATUS.left || KEY_STATUS.right ||
        KEY_STATUS.down || KEY_STATUS.up) {
        // The ship moved, so erase it's current image so it can
        // be redrawn in it's new location
        this.context.clearRect(this.x, this.y, this.width, this.height);

        // Update x and y according to the direction to move and
        // redraw the ship. Change the else if's to if statements
        // to have diagonal movement.
        if (KEY_STATUS.left) {
            this.x -= this.speed
            if (this.x <= 0) // Keep player within the screen
                this.x = 0;
        } else if (KEY_STATUS.right) {
            this.x += this.speed
            if (this.x >= this.canvasWidth - this.width)
                this.x = this.canvasWidth - this.width;
        } else if (KEY_STATUS.up) {
            this.y -= this.speed
            if (this.y <= this.canvasHeight / 4 * 3)
                this.y = this.canvasHeight / 4 * 3;
        } else if (KEY_STATUS.down) {
            this.y += this.speed
            if (this.y >= this.canvasHeight - this.height)
                this.y = this.canvasHeight - this.height;
        }
    }
    this.draw();
    if (KEY_STATUS.space && counter >= fireRate) {
        this.fire();
        counter = 0;
    }
};

this.fire = function () {
    this.bulletPool.getTwo(this.x + imageRepository.ship.width / 10, this.y, 3);
    this.bulletSoundPool.get();
};
}

Pool class。

function Pool(maxSize) {
var size = maxSize;
var pool = [];
var type = "";

// This design enables us to not need to create an object each loop
this.init = function (obj) {
    if (obj === "bullet") {
        type = "bullet";
        for (var i = 0; i < size; i++) {
            var bullet = new Bullet("bullet");
            bullet.init(0, 0, imageRepository.bullet.width, imageRepository.bullet.height);
            bullet.collidableWith = "enemy";
            bullet.type = "bullet";
            pool[i] = bullet;
        }
    }
    else if (obj === "enemy") {
        type = "enemy";
        for (var i = 0; i < size; i++) {
            var enemy = null;
            var rand = Math.floor(Math.random() * 10);
            if (rand < 8) {
                enemy = new Enemy("enemy", 0, 10);
                enemy.init(0, 0, imageRepository.enemy.width, imageRepository.enemy.height);
            }
            else {
                enemy = new Enemy("enemy2", 2, 15);
                enemy.init(0, 0, imageRepository.enemy2.width, imageRepository.enemy2.height);
            }
            enemy.collidableWith = "ship";
            enemy.type = "enemy";
            pool[i] = enemy;
        }
    }
    else if (obj === "boss_bullet") {
        type = "boss_bullet";
        for (var i = 0; i < size; i++) {
            var bullet = new Bullet("boss_bullet");
            bullet.init(0, 0, imageRepository.boss_bullet.width, imageRepository.boss_bullet.height);
            bullet.collidableWith = "ship";
            bullet.type = "bullet";
            pool[i] = bullet;
        }
    }
    else if (obj === "lives") {
        type = "lives";
        for (var i = 0; i < size; i++) {
            var lives = new Lives();
            lives.init(imageRepository.background.width - ((i + 1) * imageRepository.lives.width) - 10,
                imageRepository.background.height - imageRepository.lives.height - 10,
                imageRepository.lives.width, imageRepository.lives.height);
            pool[i] = lives;
        }
    }
};

// Return pool attribute for usage in checking collision
this.getPool = function () {
    var res = [];
    for (var i = 0; i < pool.length; i++) {
        if (pool[i].alive) {
            res.push(pool[i]);
        }
    }
    return res;
};

this.get = function (x, y, speed) {
    if (pool[size - 1] instanceof Bullet) {
        if (!pool[size - 1].alive) {
            pool[size - 1].spawn(x, y, speed);
            pool.unshift(pool.pop());
        }
    }
    else if (pool[size - 1] instanceof Enemy) {
        if (!pool[size - 1].alive) {
            if (!(pool[0].alive && pool[0].y <= pool[0].height)) {
                pool[size - 1].spawn(x, y, speed);
                pool.unshift(pool.pop());
            }
        }
    }
};

this.getTwo = function (x, y, speed) {
    if (type === "bullet") {
        if (!pool[size - 1].alive && !pool[size - 2].alive) {
            this.get(x, y, speed);
            this.get(x + (imageRepository.ship.width * 8) / 10
                - imageRepository.bullet.width, y, speed);
        }
    }
    else if (type === "boss_bullet") {
        if (!pool[size - 1].alive && !pool[size - 2].alive) {
            this.get(x, y, speed);
            // This will have the center of boss as the center between two bullets
            // x + 2 * (imageRepository.boss.width / 2 - x) - imageRepository.boss_bullet.width
            this.get(x + imageRepository.boss.width * 3 / 5 - imageRepository.boss_bullet.width,
                y, speed);
            console.log(x);
            console.log(x + imageRepository.boss.width * 3 / 5 - imageRepository.boss_bullet.width);
        }
    }
};

this.animate = function () {
    for (var i = 0; i < size; i++) {
        if (pool[i].alive) {
            if (pool[i].draw()) {
                pool[i].clear();
                pool.push((pool.splice(i, 1))[0]);
            }
        }
        else
            break;
    }
};

this.getSize = function () {
    return size;
};

this.setSize = function (input) {
    size = input;
};

this.decreaseLives = function () {
    if (size >= 1) {
        if (pool[size - 1] instanceof Lives) {
            pool[size - 1].setDamaged(true);
            size--;
        }
    }
};

this.increaseLives = function(){
    if (pool[size - 1] instanceof Lives){
        pool[size - 1].setDamaged(true);
        size++;
    }
};
}

生活课。

function Lives() {
this.alive = true;
this.damaged = false;

this.draw = function () {
    this.context.clearRect(this.x, this.y, this.width, this.height);
    this.context.drawImage(imageRepository.lives, this.x, this.y);
    if (this.damaged)
        return true;
}

this.clear = function () {
    alive = false;
    this.x = -1 * this.width;
    this.y = -1 * this.height;
}

this.getDamaged = function(){
    return this.damaged;
}

this.setDamaged = function(input){
    this.damaged = input;
}
}

1 个答案:

答案 0 :(得分:0)

getPool是一个函数,你需要调用它:

if (game.ship.livesPool.getPool()[0].getDamaged())
//                             ^^