简体   繁体   English

2D Javascript数组TypeError

[英]2D Javascript Array TypeError

[EDIT] Full application available at: http://bit.ly/1CGZzym [编辑]完整的应用程序可在以下位置获得: http : //bit.ly/1CGZzym

I'm receiving the error: 我收到错误:

Uncaught TypeError: Cannot set property '0' of undefined

with the following code. 使用以下代码。 I believe it is due to my not declaring the child array of the 2D array properly but I am really confused as to where I should be declaring this. 我相信这是由于我没有正确声明2D数组的子数组,但是我对于应该在哪里声明这一点感到非常困惑。 Any ideas would be excellent. 任何想法都很好。

// Create an array for the tiles we're about to draw
var tileArray = []

is declared out side of the function. 在函数的外部声明。

I assume it is because I am trying to create child elements within each [col] so I guess I need to declare each col number somewhere but nothing I attempt seems to be working. 我猜想是因为我正在尝试在每个[col]中创建子元素,所以我想我需要在某个地方声明每个col编号,但是我似乎没有任何尝试。

function drawGrid()
        {

            // Draw diamond grid

            var col = 0;
            var row = 0;

            topTileX = (viewWidth/2);
            topTileY = 0;

            var nextX = 0;
            var nextY = 0;

            var getCols = 0;

            while (topTileX > -1)
            {

                tileArray[col][row] = new DiamondTile(topTileX, topTileY, tileWidth, true, col, row);
                tileArray[col][row].draw();

                while (tileArray[col][row].xPos + tileArray[col][row].tileWidth < (viewWidth) + tileWidth)
                {
                    col++;

                    nextX = tileArray[col-1][row].xPos + tileArray[col-1][row].tileWidth / 2;
                    nextY = tileArray[col-1][row].yPos + tileArray[col-1][row].tileHeight / 2;

                    tileArray[col][row] = new DiamondTile(nextX, nextY, tileWidth, true, col, row);
                    tileArray[col][row].draw();

                    if (col == getCols)
                            {
                                break;
                            }
                }

            row++;
            getCols = col;
            col = 0;

            topTileX = topTileX - tileWidth/2;
            topTileY = topTileY + tileHeight/2;

            }
        };

For the purpose of demonstration, the DiamondTile function is as follows: 为了演示的目的,DiamondTile函数如下:

function DiamondTile(xPos,yPos,width,interactive,myCol,myRow)
        {
            // Set x and y position for this sprite
            this.xPos = xPos;
            this.yPos = yPos;
            this.myRow = myRow;
            this.myCol = myCol;

            // Used for AI pathfinding
            this.isObstacle = false;
            this.isStart = false;
            this.isEnd = false;

            this.gValue = 0;
            this.hValue = 0;
            this.fCost = 0;

            this.tileWidth = width;
            this.tileHeight = this.tileWidth/2;

            var self = this;

            // Create sprite
            this.spriteObj = new PIXI.Sprite(grass);
            this.spriteObj.interactive = interactive;

            this.spriteObj.anchor = new PIXI.Point(0.5,0);

            this.spriteObj.hitArea = new PIXI.Polygon([
                new PIXI.Point(0,0),
                new PIXI.Point(100,50),
                new PIXI.Point(0,100),
                new PIXI.Point(-100,50)
                ]);

            this.spriteObj.mouseover = function()
            {
                if (self.spriteObj.tint == 0xFFFFFF)
                {
                self.spriteObj.tint = 0xA7E846;
                }

                text2.setText(self.myCol + "," + self.myRow + " Start: " + self.isStart);
            }
            this.spriteObj.mouseout = function()
            {
                if (self.spriteObj.tint == 0xA7E846)
                {
                self.spriteObj.tint = 0xFFFFFF;
                }
            }
            this.spriteObj.click = function()
            {
                if (startStage === true)
                {
                    startStage = false;
                    self.isStart = true;
                    self.spriteObj.tint = 0x1AFF00;
                    text.setText("Now select an end point");
                    endStage = true;
                    return true;
                }
                if (endStage === true)
                {
                    endStage = false;
                    self.isEnd = true;
                    self.spriteObj.tint = 0xFF0000;
                    text.setText("Now place some obstacles");
                    obsStage = true;
                    return true;
                }
                if (obsStage ===true)
                {
                    self.isObstacle = true;
                    self.spriteObj.tint = 0x3B3B3B;
                    text.setText("Press 'C' to calculate path");
                    return true;
                }

            }


        };

That is a multi-dimensional array and you have not initialized the first dimension array correctly. 那是一个多维数组,您尚未正确初始化第一维数组。 In the while loop you have to initialize the first dimension to be able to access a second dimension element with an index: 在while循环中,您必须初始化第一个维度才能访问带有索引的第二个维度元素:

while (topTileX > -1)
{
    if (tileArray[col] == null)
        tileArray[col] = [];

    tileArray[col][row] = new DiamondTile(topTileX, topTileY, tileWidth, true, col, row);
    tileArray[col][row].draw();

    // omitted other code for brevity
}

Javascript arrays are dynamic and it's enough to initialize the first dimension array elements in this case. Javascript数组是动态的,在这种情况下,足以初始化第一维数组元素。 You don't have to initialize the individual items in the second dimension. 您不必在第二维中初始化各个项目。

Update : here is a fiddle with working code http://jsfiddle.net/0qbq0fts/2/ 更新 :这是一个工作代码http://jsfiddle.net/0qbq0fts/2/的小提琴

In addition your semantics is wrong. 另外,您的语义是错误的。 By the book, the first dimension of a 2-dimensional array should be rows, and the second dimension should be columns. 根据这本书,二维数组的第一维应该是行,第二维应该是列。

You have to explicit create the elements representing the second dimension, eg: 您必须显式创建代表第二维的元素,例如:

function make2DArray(rows, cols) {
    var r = Array(rows);
    for (var i = 0; i < rows; ++i) {
        r[i] = Array(cols);
    }
    return r;
}

If you don't know in advance how many columns, just use this: 如果您事先不知道多少列,请使用以下命令:

function make2DArray(rows) {
    var r = Array(rows);
    for (var i = 0; i < rows; ++i) {
        r[i] = [];
    }
    return r;
}

The individual rows can each have independent lengths, and will grow as you add values to them. 每个单独的行可以具有独立的长度,并且在向它们添加值时会不断增长。

Similarly, if you just need to add a new (empty) row, you can just do: 同样,如果只需要添加新的(空)行,则可以执行以下操作:

tileArray.push([]);

This should probably me a comment, but SO has crashed on my side. 这可能应该是我的评论,但是SO崩溃了。 JavaScript might be throwing an exception on your stated line, but the problem may be with the 'DiamondTile' function. JavaScript可能会在您声明的行上引发异常,但问题可能出在'DiamondTile'函数上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM