javascript中函数的可访问性

javascript中函数的可访问性,javascript,inheritance,Javascript,Inheritance,这是一个来自javascript的问题 if (game.ship.livesPool.getPool[0].getDamaged()) 我在另一个函数中发现了一个未定义的getDamaged()函数错误 game = new Game(); 游戏在此函数之外定义为全局变量 this.ship = new Ship(); 船是在游戏类中定义的 this.livesPool = new Pool(); var pool = []; this.getPool = function(){

这是一个来自javascript的问题

    if (game.ship.livesPool.getPool[0].getDamaged())
我在另一个函数中发现了一个未定义的getDamaged()函数错误

game = new Game();
游戏在此函数之外定义为全局变量

this.ship = new Ship();
船是在游戏类中定义的

this.livesPool = new Pool();
var pool = [];
this.getPool = function(){
  return pool;
}
livesPool是在Pool类中定义的。pool是在pool类中定义的数组。 getPool函数将返回此数组

pool[i] = new Lives();
每个池[i]将被分配池类中的Lives对象

this.getDamaged = function(){
    return this.damaged;
}
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++;
    }
};
}
getDamaged()函数在Lives类中是这样定义的

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;
}
}
为什么它告诉我这个函数是未定义的

游戏课

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

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

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

我认为你需要展示你的实际代码。你试图一行一行地描述你的代码,但它不起作用。请与我们共享您的实际代码。现在添加。很抱歉。
if (game.ship.livesPool.getPool()[0].getDamaged())
//                             ^^