0
votes

I created a level editor for web game, I can build, save, load and play levels. Now I want to edit some levels but I have a weird situation.

I export a level as a single array, it looks like this 3,4,5,5,7,89,4,2,1...and those numbers represent frames. (tile-based).

Now if I want to edit this level and save it again, I need a level to be described as multidimensional array.

Actually, when I save the level I have a string that describes my map, then I convert string to array.

So can you tell me (if possible), how to convert this array1 (or string) to array2? Lets say I have only 25 tiles (map from level editor is array1)

array1 =
1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,5,5,5,5

I need this:

array2 =
[
[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3],
[4,4,4,4,4],
[5,5,5,5,5]
];

UPDATE:

So I need 2d array to build level container. I do not have experience with tile based games, but here you can see what I do. Let's say I have 2d array and this is how I create a new level container:

            for (i = 0; i < array2.length; i++)
            {
                for (var j = 0; j < array2[i].length; j++)
                {
                    tile = new Tile();
                    tile.name = "" + i + j;
                    tile.x = j * tile.width;
                    tile.y = i * tile.height;
                    levelContainer.addChild(tile);
                    tile.gotoAndStop(array2[i][j]+1);
                    tile.addEventListener(MouseEvent.MOUSE_DOWN,
                    buildingLeve);
                }
            }

            addChild(levelContainer);

I have tried to get 2d array from single array as Rudolfwm and Marcela suggested, but when I want to edit a level container using new array2, my tiles go on wrong frames.

For example, if correct frame is 1, tile goes to frame 11, This code above (building level) works if I create my own 2d array, but not if I convert string to 2d array as suggested.

2
Does your array1 actually contain newline characters, or is it one long string of comma-separated values? - Marcela
It is a long string of comma separated values, like this : 1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5 - User 987

2 Answers

2
votes

Try array1[x+y*row] which gives the same result as copying all your data to array2[x][y].

Or if you insist on 2d arrays:

var array2 = new Array(row);
for (var y = 0; y < row; y++) {
 array2 [y] = new Array(column);
 for(var x=0; x < column; x++) {
    array2 [y][x]=array1[x+y*row];
 }
}
1
votes

You start with a String and convert that into an Array using String.split().

Once you have a temporary array, you use a nested loop to populate the final array (arr21).

var row:int = 5;
var column:int = 5;

var arr1:String = "1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5";
var tempArr:Array = arr1.split(',');
var arr2:Array = [];
for(var i:int = 0; i < row; i++)
{
    arr2[i] = []; // create a new row array
    for(var j:int = 0; j < column; j++)
    {
        // grab the first item from the temp array and push it onto the row array
        arr2[i].push(tempArr.shift());
    }
}

NOTE: This is not optimized, and could become quite laggy with larger level maps. This is just to give you an idea of where you can start.