0

What is wrong with this Array Initialization I am getting syntax errors This class hold levels. I cant figure out why its not working. Previously i used to intialize 2d arrays by first declaring an array of certain length then assigning arrays to the elemnets of main array.But when i used this method i am not getting it right

package  {

public class Levels {

    public var Level1:Array = new Array();
    public var Level2:Array = new Array();
    public var Level3:Array = new Array();
    public var Level4:Array = new Array();
    public var Level5:Array = new Array();
    public var Level6:Array = new Array();

    public function Levels() {
        Level1[] =((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level2[] =((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level3[] = ((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level4[]= ((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level4[]= ((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level5[]= ((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
        Level6[]= ((1,1,1,1,1,1,1,1),
                   (0,1,1,1,1,1,1,0),
                   (0,0,1,1,1,1,0,0),
                   (0,0,1,1,1,1,0,0),
                   (1,1,1,1,1,1,1,1));
    }

}

}
3
  • Remove square brackets from assignment, and replace round brackets with square brackets on the right side of assignment. Level1 = [[1,1,1,1,1,1,1,1],[0,1,1,1,1,1,1,0],...]; Commented May 22, 2014 at 5:10
  • @Vesper only remoeing square brackets worked Level1 = ((1,1,..),(0,1,..)) Commented May 22, 2014 at 9:01
  • 1067: Implicit coercion of a value of type int to an unrelated type Array.. I don't know what you are building with, but this does not work :) Commented May 22, 2014 at 9:10

1 Answer 1

1

Your code looks like PHP or some that kind of language - Action Script Array has different approach. You can either use what @Vesper said:

Level1 = [ // array
    [1, 1, 0, 1], // first element, child array
    [0, 0, 0, 0], // second element, child array
    [0, 1, 1, 1]
];

Or you can use push:

Level1.push([0, 1, 0, 1]); // first element, child array
Level1.push([1, 1, 1, 1]); // second element, child array

And just to mention, if you want to use brackets to get/set element of the array, you must provide index:

Level1[0] = [0, 0, 0]; // set first element
Level1[1] = [1, 1, 1]; // set second element
trace(Level[2]); // throws error in your case as there is no such element
Sign up to request clarification or add additional context in comments.

1 Comment

Removing the square brackets after array name worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.