误读多维数组

时间:2014-05-11 00:27:42

标签: c# multidimensional-array

我试图让一个循环读取一个单独的类中定义的多维数组,以便在游戏开始时放置对象。

这是LevelStart类的Start函数代码:

void Start () {
    //Get Level
    Levels levels = ScriptableObject.CreateInstance<Levels>();
    level = levels.GetLevel (levelName);
    x = level.GetLength(1);
    y = level.GetLength (2);

    temp = GameObject.Find ("temp").GetComponent<Temp>();
    lPos = temp.GetLevelNum() - 1;   //Chosen Level

    //Read array and place objects
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            if (level[lPos,i,j] == 1) {
                Debug.Log ("Spawn Point");
            }
            else if (level[lPos,i,j] == 2) {
                Debug.Log ("Hole");
            }
            else if (level[lPos,i,j] == 3) {
                Debug.Log ("Obstacle");
            }
            else 
                Debug.Log ("Open Space");
        }
    }
}

持有数组的类是Levels.cs:

public int[,,] cave = new int[,,] {
    {   //Level 1
        { 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
        { 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0, 0},
        { 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
        { 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0},
        { 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0},
        { 0, 0, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0},
        { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
        { 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0},
        { 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        { 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 3, 0, 0, 0}
    },

    {   //Level 2
        { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
        { 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
        { 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0 },
        { 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 },
        { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 }
    }
};

它正确地读取了大部分数组但是[0,0,0]和[0,0,12]等位置在应该为0时返回3

提前致谢

1 个答案:

答案 0 :(得分:0)

我觉得你很可能只是使用了错误的关卡编号 - 未显示的代码。我建议使用print语句来确保temp.GetLevelNum()和lPos是你认为它们应该是的数字。