在AS3二维数组问题 [英] Problem with 2d Array in AS3

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

问题描述

我遇到一个奇怪的问题。我想生成的元素,1或0(填充随机)的价值矩阵。我保存的值转换为二维数组。这是第一个框架上code。一切似乎是工作的罚款。

I am encountering a strange problem. I am trying to generate a matrix with value of elements either 1 or 0 (filled randomly). I am storing the values into a 2D array. This is code on the first frame. Everything seems to be working fine.

var multiArr:Array = new Array([2], [2]);


    function generateMatrixXML() {

    for(var i:uint = 0; i < 2; i++)
    {
        for(var j:uint = 0; j < 2; j++)
        {
            multiArr[i][j] = getRandomNumber(0,1);;

        }
    }
    trace(multiArr);
    }

    function getRandomNumber(lower,upper):Number {
        return Math.floor(Math.random()*(1+upper-lower))+lower;
    }


    generateMatrixXML();

在我的行和列的值更改为3我得到一个错误。

When I change the value of row and column to 3 I get an error.

var multiArr:Array = new Array([3], [3]);


function generateMatrixXML() {

for(var i:uint = 0; i < 3; i++)
{
    for(var j:uint = 0; j < 3; j++)
    {
        multiArr[i][j] = getRandomNumber(0,1);;

    }
}
trace(multiArr);
}

function getRandomNumber(lower,upper):Number {
    return Math.floor(Math.random()*(1+upper-lower))+lower;
}


generateMatrixXML();

类型错误:错误#1010:术语是不确定的,没有属性。     在matrixArray_fla :: MainTimeline / generateMatrixXML()     在matrixArray_fla :: MainTimeline /帧1()

TypeError: Error #1010: A term is undefined and has no properties. at matrixArray_fla::MainTimeline/generateMatrixXML() at matrixArray_fla::MainTimeline/frame1()

有任何想法,什么是错误的原因

Got any idea as to what is the reason for error

推荐答案

这行:

var multiArr:Array = new Array([3], [3]);

时创建包含两个阵列,其中每一个具有含有数3.这意味着,当你到第三次迭代 multiArr [I] 是一个元素的数组未定义。这显然​​是如何定义的AS3数组一个误区 1

Is creating an array containing two arrays, each of which has one element containing the number 3. That means that when you get to the third iteration multiArr[i] is undefined. This is clearly a misunderstanding of how to define arrays in AS31

您确实需要在循环中创建数组:

You actually need to create your arrays within the loop:

var multiArr:Array = [];
var iterations:int = 3;

function generateMatrixXML() {
    for(var i:uint = 0; i < iterations; i++)
    {
        multiArr[i] = [];
        for(var j:uint = 0; j < iterations; j++)
        {
            multiArr[i][j] = getRandomNumber(0,1);

        }
    }
    trace(multiArr);
}

function getRandomNumber(lower,upper):Number {
    return Math.floor(Math.random()*(1+upper-lower))+lower;
}

generateMatrixXML();

注:1:的Flash编码实践要求你定义一个新的数组与 [] 注释:

Footnote 1: Flash coding practises require you to define a new array with the [] notation:

var arr1:Array = []; // create a new array
var arr2:Array = [1]; // create an array with  one element, the number 1
var arr3:Array = new Array(1); // defines a new array with a pre-set length of 1
var arr4:Array = new Array(1,2); // defines a new array with two elements, the numbers 1 and 2

最后一个例子是认为它与第三个例子造成的混乱,因为在AS3不好的编码实践。

The last example is considered a bad coding practise in AS3 because of the confusion it causes with the third example.

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

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