0

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? Commented Aug 11, 2014 at 20:50
  • 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 Commented Aug 11, 2014 at 20:55

2 Answers 2

2

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];
 }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I need 2d array and nested for loop, else I have even bigger problems.
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]; } }
I have tried your code, but somethings wrong and I do not know what, I have updated my question with an example.
the way I understand your code, your mapping from tiles to numbers (array1) goes wrong. From what I see from the numbers you have they are actually ROW numbers, correct? why dont you replace : tile.gotoAndStop(array2[i][j]+1); with tile.gotoAndStop(i+1); since that is what you are doing?
I do not have any problems when I save a level. When I want to play the level, I load a map using single array and use tile.gotoAndStop(i+1). But if I want to edit the level I need to convert that single array to multidimensional array. So I create a new levelContainer using for loops and tile.gotoAndStop(array2[i][j]+1) as you can see above. The only problem is how to make single array (1,2,3,3,4,6,3,5,6) looks like 2d array [[1,2,3],[3,4,6],[3,5,6]]
1

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.

3 Comments

I copy and paste your code, but I have another problem now, tiles go on wrong place after using your arr2. I have tried to display arr2 in textField but I see single array instead. Yet, I am not sure if textField should display 2d array.
@user2933219 Arr2 is a 2d array. Try : trace(arr2[1]); Marcela's code works. If your tiles are at a wrong place, here mine are correctly placed.
It works now, it is weird to me, cause when I create my own 2d level I use tile.gotoAndStop(array2[i][j]+1); and everything is fine. I tried the same with arr2 and I see tiles on wrong frames. It simple add "1" to current frame, so if tile.currentFrame is 4 after loading a map tile.currentFrame is 41. So now I use tile.gotoAndStop(array2[i][j]) and somehow it works. I do not understand this but it works, so I can accept it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.