Im trying to make a level editor for the game. Now I can create a new map (using mouse) and click "generate" button to trace map array (string). After that I can simple copy the code from the output and use it to create a new level.
Lets say I have a class called NewLevel.as
I create a new array and paste code from output window, so I have 2d array. Then adding tiles to stage using for loops.
var array:Array =
// code below is what I get in output window
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
];
for (var row:int = 0; row < array.length; row++)
{
for (var column:int = 0; column < array[row].length; column++)
{
var tile = new Tile();
addChild(tile);
tile.x = column * tile.width;
tile.y = row * tile.height;
tile.gotoAndStop(array[row][column] +1);
}
}
It works without problems,this gives me the map I created using level editor. but what I want is that players input their "map code" and load the map they created. I guess you have seen that in many games.
I have a textarea so users can input their string, how can I convert their input to 2d array and load it (as you see in example)? It should be 2d array.
I also added event listener to textarea
textarea.addEventListener(Event.CHANGE, changes);
function changes(e:Event):void
{
// convert input text to 2d array to build a new map
// Do not know how to get input to use with JSON
var myStr = levelTextarea.text;
var a2:Array = JSON.parse(myStr) as Array;
trace( a2 );
}
You can use JSON for this kind of job, this class is available in Flash Player 11.
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var a:Array = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
];
var txt:String = JSON.stringify( a )
trace( txt );
var a2:Array = JSON.parse( txt ) as Array;
trace( a2 );
}
}
}