TypeError:无法读取未定义的属性'#'在简单的JavaScript游戏中

时间:2013-08-07 23:39:04

标签: javascript typeerror

我和我的一个朋友正在进行一场小型战舰游戏。我们认为这是一种很好的做法。我们有一个10X10的二维数组,全部用零填充。我们会将这些零改为我们的船只所在的地方。我做了一个放置1X5船的功能。该函数提示用户输入两个坐标,并将这些坐标分配给函数外部的变量。它获取起点的坐标,并询问玩家希望船舶休息的方向。如果没有障碍物,应该放置船。如果有一些功能应该重启。问题是我得到了这个错误。 #被替换为要求垂直坐标位置的提示中的数字。

这是错误。 TypeError:无法读取未定义的战舰游戏中的属性“#”

这是功能

function firstShip(){
    window.alert("We will be placing your 1 by 5 length ship.  Take note that you are playing on a 10 by 10 board.");
    userX = parseInt(prompt("Horizontal Coordinate position for the first unit of a ship")-1);
    userY = parseInt(prompt("Vertical Coordinate position for the first unit of a ship")-1);
    direction = prompt("Now choose the direction you want the rest of your ship to face.  You may   use the words up, down left, or right.").toLowerCase(); 

    //Making sure the ship will fit and nothing is already there!
    if ((userX+4)>9 && direction=== "right"){
     window.alert("You are too close to the right edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userX-4)<0 && direction=== "left"){
     window.alert("You are too close to the left edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userY+4)>9 && direction=== "down"){
     window.alert("You are too close to the bottom edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userY-4)<0 && direction=== "up"){
     window.alert("You are too close to the top edge of the board to do that. Restarting...");
     firstShip();
    }
    else if (user[userX][userY] === 1) {
        window.alert("Coordinate already used. Please try again");
        firstShip();
    } 

    else if (user[userX][userY] === null || user[userX][userY] === ""){
        window.alert("That coordinate isn't on the board. Restarting...");
        firstShip();
    }

    else if(direction !=="up" || direction !=="down" || direction !=="left" || direction !=="right") {
        for(i=1; i<5; i++){
            if(user[userX+i][userY] === 1 && direction=== "right"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX-i][userY] === 1 && direction=== "left"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX][userY+i] === 1 && direction=== "down"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX][userY-i] === 1 && direction=== "up"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(isThere===true){
                isThere = false;
                firstShip(); 
                return false;
            }
            else{
                user[userX][userY] = 1;
            }
        }

    }
    else{
        window.alert("Sorry but you didn't type in the direction you wanted your ship to go correctly. Restarting...");
        firstShip();
    }


    // Building Ship 1x5
    for(i=1; i<4; i++){

        if (direction==="up"){
            user[userX][userY-i] =1;
        }
        else if (direction==="down"){
            user[userX][userY+i] =1;
        }
        else if (direction==="left"){
            user[userX-i][userY] =1;
        }
        else if (direction==="right"){
            user[userX][userY+i] =1;
        }
    }
}
firstShip();
//Here is the new cpu creation. There may be a way to shorten the if statements, but other than that, its all set

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您正在使用负数组索引。

如果您在此处尝试代码:http://plnkr.co/edit/g4i1QAY6COU18XTK6QZI?p=preview

并输入1,1,对于问题的答案,你会看到你在script.js第28行收到错误:

if(user[userX-i][userY] === 1 && direction=== "left"){

错误是:

Uncaught TypeError: Cannot read property '0' of undefined 

userX = 0且i = 1会发生什么,所以我们正在评估user[-1][0]。由于用户[-1]未定义,这就是您看到错误的原因。