forum > Problem title array

  • I know to declare an array:

    var a:Array<Dynamic>=[....];

    I am making a tile.I tried:
    class Map 
    {
        public static var myMap:Array<Int>;
        public function new() 
        {
             myMap = [
               [1, 1, 1, 1, 1, 1, 1, 1], 
               [1, 0, 0, 0, 0, 0, 0, 1], 
               [1, 0, 1, 0, 0, 0, 0, 1], 
               [1, 0, 0, 0, 0, 1, 0, 1], 
               [1, 0, 0, 0, 0, 0, 0, 1], 
               [1, 1, 1, 1, 1, 1, 1, 1]
               ];
        }
        
    }

    I get Array<Int> should be Int.
    How to declare this array in haxe?
    thanks

  • public static var myMap:Array<Array<Int>>;
  • However since it's a static var it scopes to the class not the instance so you don't need to define it in new ( each instance will effectively be redefining it to the same thing ), not sure what difference inline makes in this situation but maybe...

    public static var myMap:Array<Array<Int>> = [
               [1, 1, 1, 1, 1, 1, 1, 1], 
               [1, 0, 0, 0, 0, 0, 0, 1], 
               [1, 0, 1, 0, 0, 0, 0, 1], 
               [1, 0, 0, 0, 0, 1, 0, 1], 
               [1, 0, 0, 0, 0, 0, 0, 1], 
               [1, 1, 1, 1, 1, 1, 1, 1]
               ];

    If you only need 1 and 0 then Bool maybe better, but more typing :)

< Prev | Page 1 / 1 | Next >