简体   繁体   中英

How to make 2D Vector in AS3

I'm able to do it like this:

        var levelVector = new Vector.<Vector.<int>>();
        levelVector[0] = new Vector.<int>();
        levelVector[1] = new Vector.<int>();
        levelVector[2] = new Vector.<int>();


        levelVector[0][0] = 0;
        levelVector[0][1] = 0;
        levelVector[0][2] = 5;
        levelVector[0][3] = 0;
        levelVector[0][4] = 0;
        levelVector[0][5] = 0;
        levelVector[1][0] = 0;
        levelVector[1][1] = 0;
        levelVector[1][2] = 5;
        levelVector[1][3] = 0;
        levelVector[1][4] = 0;
        levelVector[1][5] = 0;
        levelVector[2][0] = 0;
        levelVector[2][1] = 5;
        levelVector[2][2] = 2;
        levelVector[2][3] = 2;
        levelVector[2][4] = 2;
        levelVector[2][5] = 2;

but I'd rather have something like this:

levelVector = [[0,0,3,2,1],[1,1,2,2,2],[3,2,2,1,2]];

which would be even more useful like this:

levelVector = [
              [0,0,3,2,1],
              [1,1,2,2,2],
              [3,2,2,1,2]
              ];

because now it actually reads like a 2D array, or a grid, which would be very helpful. I think I can do it like this with arrays, so is there a way with Vectors? Filling in each value for a large 2 dimensional vector, each with its own line of code is hard to look at or make sense of or write. Am I missing something or this just a short-fall of vectors?

Thanks!

Yes, but you cannot directly convert a 2D array (or array of arrays) into a vector of vectors. But you can do like this:

var levelVector:Vector.<Vector.<int>>=Vector.<Vector.<int>>(
    [
        Vector.<int>([0,0,3,2,1]),
        Vector.<int>([1,1,2,2,2]),
        Vector.<int>([3,2,2,1,2])
    ]
);

As others have said (and to explain it further, since you seem to be a little confused) you can use the Vector global function to convert an Array to a Vector :

var level:Vector.<int> = Vector.<int>([
    0, 0, 3, 2, 1,
    1, 1, 2, 2, 2,
    3, 2, 2, 1, 2
]);

And you can nest calls to this function to form a 2D vector (which you don't really need -- I recommend you just use a flat vector):

var level:Vector.<Vector.<int>> = Vector.<Vector.<int>>([
    Vector.<int>([0, 0, 3, 2, 1]),
    Vector.<int>([1, 1, 2, 2, 2]),
    Vector.<int>([3, 2, 2, 1, 2])
]);

Or you can simply use pre-populated vector instantiation as shown in the documentation :

var level:Vector.<Vector.<int>> = new <Vector.<int>>[
    new <int>[0, 0, 3, 2, 1],
    new <int>[1, 1, 2, 2, 2],
    new <int>[3, 2, 2, 1, 2]
];

Lastly, you could just write a function to convert a 2D array into a 2D vector:

function createLevelVector(level:Array):Vector.<Vector.<int>> {
    var levelVector:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(level.length);
    for (var i:int = 0; i < level.length; i++) {
        levelVector[i] = Vector.<int>(level[i]);
    }
    return levelVector;
}

var level:Vector.<Vector.<int>> = createLevelVector(
    [
        [0, 0, 3, 2, 1],
        [1, 1, 2, 2, 2],
        [3, 2, 2, 1, 2]
    ]
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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