Jquery Html5游戏 - 无法读取'1'未定义的属性

时间:2013-12-24 10:57:08

标签: javascript jquery html5 html5-canvas

我用jquery和html画布创建一个游戏,但我不断收到错误。 当游戏只运行Zombie和SecondZombie时,BossZombie保持不动,我收到错误

Uncaught TypeError: Cannot read property '1' of undefined on line 159

是 - 如果(Secondzombies [i] [1]> player_y)

但僵尸移动得很好,不动的老板(需要移动)

我在游戏开始时创建了3个敌人,然后通过setInterval

进行更新

我要更新的循环如下

function DrawZombies() { // Draw and update all zombies
for (var i = 0; i < zombies.length; i++) {
    ctx.drawImage(zombie, zombies[i][0], zombies[i][1]);
}for (var i = 0; i < Secondzombies.length; i++) {
    ctx.drawImage(SecondZombie, Secondzombies[i][0], Secondzombies[i][1]);
}for (var i = 0; i < BossZombies.length; i++) {
    ctx.drawImage(BossZombie, BossZombies[i][0], BossZombies[i][1]);
}

// move Zombies
for (var i = 0; i < zombies.length; i++) 
{
    // zombies 1
    if (zombies[i][1] > player_y) { // move zombie towards player on the y axis
        zombies[i][1] -= zombies[i][4];
    } else {
        zombies[i][1] += zombies[i][4];
    } 

    if (zombies[i][0] > player_x) {
        zombies[i][0] -= zombies[i][4]; // move zombie towards player on the x axis
    } else {
        zombies[i][0] += zombies[i][4];
    }
}

for (var i = 0; Secondzombies.length; i++)
{
    // zombies 2
    if (Secondzombies[i][1] > player_y) { // move zombie towards player on the y axis
        Secondzombies[i][1] -= Secondzombies[i][4];
    } else {
        Secondzombies[i][1] += Secondzombies[i][4];
    } 

    if (Secondzombies[i][0] > player_x) {
        Secondzombies[i][0] -= Secondzombies[i][4]; // move zombie towards player on the x axis
    } else {
        Secondzombies[i][0] += Secondzombies[i][4];
    }
}

for (var i = 0; BossZombies.length; i++)
    {
    // zombies 3
    if (BossZombies[i][1] > player_y) { // move zombie towards player on the y axis
        BossZombies[i][1] -= BossZombies[i][4];
    } else {
        BossZombies[i][1] += BossZombies[i][4];
    } 

    if (BossZombies[i][0] > player_x) {
        BossZombies[i][0] -= BossZombies[i][4]; // move zombie towards player on the x axis
    } else {
        BossZombies[i][0] += BossZombies[i][4];
    }
}
} 

每个僵尸的创建如下

for (var i = 0; i < zombie_total; i++) {
    var TypeOfZombie = 1 + Math.floor(Math.random() * 6); // generate random number for type of zombie
    if (TypeOfZombie <= 4) { // create type one zombie
        zombies.push([Math.round(Math.random()*(width-20)/1), Math.round(Math.random()*(height-250)/1), zombie_width, zombie_height, zombie_speed]);
        zombie_x += zombie_width + 50;

    } else if (TypeOfZombie == 5) { 
        Secondzombies.push([Math.round(Math.random()*(width-20)/1), Math.round(Math.random()*(height-250)/1), Secondzombie_width, Secondzombie_height, Secondzombie_speed]);
        Secondzombie_x += Secondzombie_width + 50;
    } else { // leaves only a 6 generated so generate boss
        BossZombies.push([Math.round(Math.random()*(width-20)/1), Math.round(Math.random()*(height-250)/1), BossZombie_width, BossZombie_height, BossZombie_speed]);
        BossZombie_x += BossZombie_width + 50;
    }
}

我将补充说,所有的僵尸都是画画,但只有僵尸&amp; SecondZombies移动,BossZombie仍然停留

1 个答案:

答案 0 :(得分:1)

看起来你的for()循环就好了。

看一下这一行,例如

for (var i = 0; Secondzombies.length; i++)

这将贯穿Secondzombies中的所有项目,但它不会正确终止(即它会超出数组)因为条件“Secondzombies.length”将始终评估为'true'

或许用

替换(和Bosszombies循环)
for (var i = 0; i < Secondzombies.length; i++)