2d map creator
Create our own 2d map
This map creator use example this game:
http://flashplusplus.net/opengame.php?gamename=stickman_biker_challenges&width=670&height=550
Test map is the "Time trial #1"
CLICK | add node
ARROWS | move map
SHIFT | switch draw mode on/off
ENTER | trace map
C | clear map
D | delete dragged node
I | import test map
Q | close "mapline" and create new
/**
* Copyright NewKrok ( http://wonderfl.net/user/NewKrok )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/i7d1
*/
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class MapEditor extends Sprite {
private const TEST_MAP:String = "148<510<158<536&158<536<199<554&199<554<367<554&367<554<429<538&429<538<457<516&457<516<478<497&478<497<521<479&521<479<579<478&579<478<624<491&624<491<662<508&662<508<706<508&706<508<764<510&764<510<823<533&823<533<897<569&897<569<981<573&981<573<1053<550&1053<550<1089<504&1089<504<1114<468&1114<468<1161<432&1161<432<1221<417&1221<417<1273<419&1273<419<1342<436&1342<436<1415<467&1415<467<1455<501&1455<501<1474<544&1474<544<1512<595&1512<595<1578<605&1578<605<1643<608&1643<608<1762<577&1762<577<1797<552&1797<552<1861<541&1861<541<1927<558&1927<558<1989<558&1989<558<2046<538&2046<538<2095<495&2095<495<2167<417&2167<417<2225<388&2225<388<2310<381&2310<381<2360<393&2360<393<2419<414&2419<414<2474<410&2474<410<2523<391&2523<391<2562<354&2562<354<2628<323&2628<323<2719<322&2719<322<2793<326&2793<326<2832<346&2832<346<2898<389&2898<389<2953<397&2953<397<3073<376&3073<376<3141<329&3141<329<3182<298&3182<298<3209<267&3209<267<3278<233&3278<233<3368<228&3368<228<3438<241&3438<241<3513<286&3513<286<3566<298&3566<298<3609<321&3609<321<3659<371&3659<371<3701<445&3701<445<3769<512&3769<512<3892<553&3892<553<3960<553&3960<553<4042<532&4042<532<4042<532&4042<532<4096<489&4096<489<4152<450&4152<450<4246<422&4246<422<4338<425&4338<425<4436<452&4436<452<4497<497&4497<497<4571<525&4571<525<4712<552&4712<552<4812<541&4812<541<4870<500&4870<500<4910<447&4910<447<4986<325&4986<325<5068<224&5068<224<5159<165&5159<165<5272<161&5272<161<5308<132&5308<132<5312<96&5312<96<5265<54&5265<54<5151<37&5151<37<4798<70&4798<70<4457<89&4457<89<4293<103&4293<103<4171<90&4171<90<4069<101&4069<101<3972<136&3972<136<3871<129&3871<129<3704<81&3704<81<3549<76&3549<76<3370<40&3370<40<3221<16&3221<16<3070<35&3070<35<2893<54&2893<54<2656<96&2656<96<2473<91&2473<91<2304<68&2304<68<2170<30&2170<30<1998<52&1998<52<1837<84&1837<84<1707<74&1707<74<1602<33&1602<33<1432<24&1432<24<1257<28&1257<28<1176<49&1176<49<881<42&881<42<809<17&809<17<569<5&569<5<453<24&453<24<295<55&295<55<187<55&187<55<115<104&115<104<58<238&58<238<64<328&64<328<95<372&95<372<123<401&123<401<142<449&142<449<150<514&";
private var _info: Array = [];
private var _secondInfo: Array = [[]];
private var _nodes: Sprite;
private var _lineContainer: Sprite;
private var _newLineContainer: Sprite;
private var _back: Sprite;
private var _drawMode: Boolean = true;
private var _currentNode: int = -1;
public function MapEditor () :void {
addChild ( _back = createBack ( 465, 465 ) );
_back.x = stage.stageWidth / 2 - _back.width / 2;
_back.y = stage.stageHeight / 2 - _back.height / 2;
addChild ( _lineContainer = new Sprite );
_lineContainer.graphics.lineStyle ( 1, 0x000000, 1 );
addChild ( _newLineContainer = new Sprite );
addChild ( _nodes = new Sprite );
stage.addEventListener ( KeyboardEvent.KEY_DOWN, keyDown );
addEventListener ( MouseEvent.MOUSE_UP, addNode );
addEventListener ( Event.ENTER_FRAME, core );
}
private function createBack ( $width:Number, $height:Number ) :Sprite {
var graphic:Sprite = new Sprite;
graphic.graphics.beginFill ( 0xFFFFFF, 1 );
var rectSize:Point = new Point ( 7, 7 );
var piecePerHorizontal:uint = $width / rectSize.x;
var piecePerVertical:uint = $height / rectSize.y;
graphic.graphics.drawRect ( 0, 0, piecePerHorizontal * rectSize.x, piecePerVertical * rectSize.y );
graphic.graphics.endFill ();
for ( var i:uint = 0; i < piecePerHorizontal; i++ ) {
for ( var j:uint = 0; j < piecePerVertical; j++ ) {
if ( i % 2 == 0 && j % 2 != 0 || i % 2 != 0 && j % 2 == 0 ) {
graphic.graphics.beginFill ( 0xCBCBCB, 1 );
graphic.graphics.drawRect ( i * rectSize.x, j * rectSize.y, rectSize.x, rectSize.y );
graphic.graphics.endFill ();
}
}
}
return graphic;
}
private function addNode ( event:MouseEvent ) :void {
if ( _drawMode ) {
_info.push ( { x: mouseX, y: mouseY, node: new Node () } );
_info[_info.length - 1].node.id = _info.length - 1;
_info[_info.length - 1].node.x = _info[_info.length - 1].x;
_info[_info.length - 1].node.y = _info[_info.length - 1].y;
_nodes.addChild ( _info[_info.length - 1].node );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_DOWN, moveNode );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_UP, fixNode );
}
}
private function keyDown ( event:KeyboardEvent ) :void {
if ( event.keyCode == 37 ) {
x += 20;
_back.x -= 20;
}
if ( event.keyCode == 39 ) {
x -= 20;
_back.x += 20;
}
if ( event.keyCode == 38 ) {
y += 20;
_back.y -= 20;
}
if ( event.keyCode == 40 ) {
y -= 20;
_back.y += 20;
}
if ( event.keyCode == 68 ) {
if ( _currentNode != -1 ) {
clearMap ( false );
var tmpInfo:Array = new Array ();
var i:Number = 0;
while ( _info[i] ) {
if ( i != _currentNode ) {
tmpInfo.push ( _info[i] );
}
i++;
}
_info = new Array ();
i = 0;
while ( i < tmpInfo.length ) {
_info.push ( { x: tmpInfo[i].x, y: tmpInfo[i].y, node: new Node () } );
_info[_info.length - 1].node.id = _info.length - 1;
_info[_info.length - 1].node.x = _info[_info.length - 1].x;
_info[_info.length - 1].node.y = _info[_info.length - 1].y;
_nodes.addChild ( _info[_info.length - 1].node );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_DOWN, moveNode );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_UP, fixNode );
i++;
}
_currentNode = -1;
}
}
if ( event.keyCode == Keyboard.ENTER ) {
_drawMode = false;
traceMap ();
_newLineContainer.graphics.clear ();
}
if ( event.keyCode == Keyboard.I ) {
importMap ( TEST_MAP );
}
if ( event.keyCode == Keyboard.C ) {
clearMap ();
}
if ( event.keyCode == Keyboard.SHIFT ) {
_drawMode = !_drawMode;
_newLineContainer.graphics.clear ();
}
if ( event.keyCode == Keyboard.Q ) {
_secondInfo.push ( _info );
_info = new Array ();
_newLineContainer.graphics.clear ();
}
}
private function traceMap () :void {
var mapString:String = "";
var i:uint = 1;
while ( i < _info.length ) {
mapString += _info[i - 1].x + "<" + _info[i - 1].y + "<" + _info[i].x + "<" + _info[i].y + "&";
i++;
}
trace ( mapString );
}
private function clearMap ( $clearInfo:Boolean = true ) :void {
_lineContainer.graphics.clear ();
_newLineContainer.graphics.clear ();
removeChild ( _nodes );
addChild ( _nodes = new Sprite );
if ( $clearInfo ) {
_info = [];
x = 0;
y = 0;
_back.x = stage.stageWidth / 2 - _back.width / 2;
_back.y = stage.stageHeight / 2 - _back.height / 2;
}
}
private function importMap ( $data:String ) :void {
clearMap ();
var mapArray:Array = $data.split ( "&" );
var i:uint = 0;
while ( i < mapArray.length - 1 ) {
var tmpInfo:Array = mapArray[i].split ( "<" );
_info.push ( { x: tmpInfo[0], y: tmpInfo[1], node: new Node () } );
_info[_info.length - 1].node.id = _info.length - 1;
_info[_info.length - 1].node.x = _info[_info.length - 1].x;
_info[_info.length - 1].node.y = _info[_info.length - 1].y;
_nodes.addChild ( _info[_info.length - 1].node );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_DOWN, moveNode );
_info[_info.length - 1].node.addEventListener ( MouseEvent.MOUSE_UP, fixNode );
i++;
}
_drawMode = false;
}
private function moveNode ( event:MouseEvent ) :void {
_currentNode = event.currentTarget.id;
event.currentTarget.startDrag ( false );
}
private function fixNode ( event:MouseEvent ) :void {
_currentNode = -1;
event.currentTarget.stopDrag ();
}
private function core ( event:Event ) :void {
if ( _info[0] != null ) {
if ( _drawMode ) {
_newLineContainer.graphics.clear ();
_newLineContainer.graphics.lineStyle ( 2, 0, 0.2 );
_newLineContainer.graphics.moveTo ( _info[_info.length - 1].x, _info[_info.length - 1].y );
_newLineContainer.graphics.lineTo ( mouseX, mouseY );
}
if ( _currentNode != -1 ) {
_info[_currentNode].x = _info[_currentNode].node.x;
_info[_currentNode].y = _info[_currentNode].node.y;
}
_lineContainer.graphics.clear ();
_lineContainer.graphics.lineStyle ( 1, 0x000000, 1 );
if ( _info.length > 1 ) {
var i:uint = 1;
while ( _info[i] ) {
_lineContainer.graphics.moveTo ( _info[i - 1].x, _info[i - 1].y );
_lineContainer.graphics.lineTo ( _info[i].x, _info[i].y );
i++;
}
}
if ( _secondInfo.length > 1 ) {
i = 1;
while ( _secondInfo[i] ) {
var j:uint = 1;
while ( _secondInfo[i][j] ) {
_lineContainer.graphics.moveTo ( _secondInfo[i][j - 1].x, _secondInfo[i][j - 1].y );
_lineContainer.graphics.lineTo ( _secondInfo[i][j].x, _secondInfo[i][j].y );
j++;
}
i++;
}
}
}
}
}
}
import flash.display.Sprite;
class Node extends Sprite {
public var id:uint;
public function Node () :void {
graphics.lineStyle ( 1, 1, 1 );
graphics.beginFill ( 0x0099CC, .5 );
graphics.drawCircle ( 0, 0, 5 );
graphics.endFill ();
}
}