遇到问题理解AS3多维数组 [英] Having trouble understanding multidimensional arrays in as3

查看:198
本文介绍了遇到问题理解AS3多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一些code这样的:

Let's say I have some code like:

private function makeGrid():void
    {
        _grid = new Array();

        for(var i:int = 0; i < stage.stageWidth / GRID_SIZE; i++)
        {
            _grid[i] = new Array();

            for(var j:int = 0; j < stage.stageHeight / GRID_SIZE; j++)
            {
                _grid[i][j] = new Array();
            }
        }
    }

我不太明白是怎么回事。我拿到的第一个for循环决定了它需要的列数,并在第二个它决定了行,但我不知道为什么我在做阵列了 _grid [I] _grid [I] [J]

例如, _grid [I] =新的Array(); 获取就是所谓的16次(800px / 50像素),这样就会使16个阵列吗?为什么我需要这些,如果第二个for循环已经计算的行,我需要多少?

For instance, _grid[i] = new Array(); get's called 16 times (800px/50px), so that would make 16 arrays right? Why do I need those if the second for loops is already calculating the amount of rows I need?

推荐答案

我只是阐述已经取得的评论。比方说,你正在创建的行和列组成的二维网格和你想储存一些类型的数据在每一个细胞或电网的指定索引。

I'm just going to elaborate on what has already been commented. Let's say that you are creating a 2D grid formed of rows and columns and you wanted to store some sort of data at each "cell" or specified index of the grid.

第一步是创建第一个数组来保存任你选择先不的行或列(真正的问题,你可以调整循环或者方法)。

The first step is to create the first array to hold either the rows or columns (which you choose first doesn't really matter as you can adjust the for loops either way).

第一个循环创建一个新的行,然后在接下来的内环您填写该行的所有列(如果我们选择了列创建第一个再我们将填补列的所有行)。在这种情况下,内环与另一个阵列创建的所有列(使得它作为中提到的注释的3维阵列)。

The first for loop creates a new row, then in the next inner loop you fill all the columns of that row (if we had chosen columns to be created first then we would fill all the rows of the columns). In this case the inner loop is creating all the columns with another array (making it a 3-dimensional array as mentioned in the comments).

这样做的原因,这是对组织和轻松的样子了。如果你想查看存储在第三行的第一列中的数据将是因为这样做容易 _gird [2] [0]

The reason for doing this is for organization and easy look up. If you wanted to see the data stored in the 1st column of the 3rd row it would be as easy as doing _gird[2][0].

现在,为什么一个第三维是如 _grid [I] [J] =新的Array(); 特定于什么样的数据需要被存储在该行和列

Now as to why a 3rd dimension is made as in _grid[i][j] = new Array(); that is specific to what kind of data needs to be stored at that row and column.

这篇关于遇到问题理解AS3多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆