MMMM
MMMM

Reputation: 1317

Two Dimension Array with Custom Type Elements

I'm trying to create on my scene an n x n size matrix, each element should be a Movie Clip, named Table, already prepared in the Library.

var tables:Array.<Table> = new Array.<Table>(tablesDimension, tablesDimension);

for (var i:int = 0; i < tablesDimension; i++) {
    for (var j:int = 0; j < tablesDimension; j++) {
        var tempTable:Table = new Table();
        tempTable.x = i * 150 + 100;
        tempTable.y = j * 100 + 100;
        stage.addChild(tempTable);
        tables.push(tempTable);
        trace(tables[0][0].x);
    }   
}

A Movie Clip (in this case, Table) cannot be put in two dimensional arrays? Should I use some type conversion in my last line, at the trace(tables[0][0].x); to suggest to the compiler: it's about a Table type object?

The error message I receive: "Type parameters with non-parameterized type"

Upvotes: 0

Views: 61

Answers (2)

Fabrice Bacquart
Fabrice Bacquart

Reputation: 129

I'm not sure why you're trying to do with your first line but it's incorrect. To quote the adobe actionscript reference:

The Array() constructor function can be used in three ways.

See this adobe reference link on Array creation. Or you can use Vectors (which are typed, like you seem to want to have).

So basically you want to create an Array, that will itself contain arrays. You need to create the arrays contained in it when you go through the first one. Else you will try to push into non existing elements. Also, you need to add the index as RST said.

var tables:Array.<Table> = new Array();

for (var i:int = 0; i < tablesDimension; i++) {

    tables[i] = new Array();
    for (var j:int = 0; j < tablesDimension; j++) {
        var tempTable:Table = new Table();
        tempTable.x = i * 150 + 100;
        tempTable.y = j * 100 + 100;
        stage.addChild(tempTable);
        tables[i].push(tempTable);
    }
}
        trace(tables[0][0].x);

This should work.

Upvotes: 1

RST
RST

Reputation: 3925

I think you are missing an index. Try this

for (var i:int = 0; i < tablesDimension; i++) {
    for (var j:int = 0; j < tablesDimension; j++) {
        var tempTable:Table = new Table();
        tempTable.x = i * 150 + 100;
        tempTable.y = j * 100 + 100;
        stage.addChild(tempTable);
        tables[i].push(tempTable);
        trace(tables[0][0].x);
    }   
}

Upvotes: 0

Related Questions