COMMUNITY

Haxe 2D Array of Preset Length

#1

Hello -

I’m trying to create a 2D array to store my tile map (for my OpenFL game), and I’m having trouble creating it with a preset length. My implementation before was just

var map:Array<Array> = [[0,0,0], [0,0,0], [0,0,0]]

But now I need to adjust the array’s size according the the screen height and width of the machine being used. To do this, I need to use my constants (which I got using openfl’s Lib.current.stage.stageWidth/stageHeight methods), and I cannot figure out how.

I found a stack overflow page saying that in order to create an array with a preset size, a vector needs to be used, but I can’t find any mention of a 2D vector.

Any help would be appreciated.

(Valentin Lemière) #2

You can use array comprehension for that.

var map:Array<Array<Int>> = [
    for (_ in 0...Lib.current.stage.stageHeight) [
        for(_ in 0...Lib.current.stage.stageWidth)
            0
    ]
];
(Aleksandr Kuzmenko) #3

I guesst the vector is haxe.ds.Vector