AS3 level editor

Almost a year ago I blogged about a basic level editor for a tile based game, and now Philipp Zins from Germany show us his AS3 level editor-

I made the level editor in ActionScript 3. You can scroll through the tiles with the mouse wheel – it isn’t necessary now, but very helpful if you use many, many tiles. You can change the map size and activate the “Generate”-Button by clicking or pressing Return.You can scroll the map, too. The finished array will be traced. The tile graphics are a little different – you will see that. I think I used some german words in the code, sorry.

Prepare yourself for a quite long source code…

/*1. variables

2.UI
2.1. map: height+width options, generate button
2.2. selectable tiles

3. functions
3.1. selectable tiles
3.1.1. current tile
3.1.2 scrolling
3.2. generate button
3.2.1. on Enter, too
3.2.2. generate map
3.2.2.1. get width, height
3.2.2.2. place containers
3.2.2.3. add tiles, map array	
3.2.2.4. add scrollbars
3.2.2.5. add masks
3.2.2.6. add btnArray
3.3. edit map
3.4. scroll map
3.4.1. scroll map: y-direction
3.4.2. scroll map: x-direction
3.5. trace array
*/


//1. variables
var tile_size = 20;
var totalTiles:int = currentTile_mc.totalFrames;
var currentTile:int = 0;
var map:Array = new Array();
//Sprites, Movieclips (+Positions)
var selection_container:Sprite;
var t:Tile; 
var map_height;
var map_width;
var level_container:Sprite;
var level_container_xPos = 65;
var level_container_yPos = 60;
var map_sprite_height:int = 0;
var map_sprite_width:int = 0;
var maskedView_map:Map_Maske;
var map_mask_height:int = 340;
var map_mask_width:int = 400;
var scroll_y_container:Sprite;
var scroll_y_container_yPos = 60;
var scrolltrack_y:Scrolltrack_y;
var scrollhead_y:Scrollhead_y;
var scroll_x_container:Sprite;
var scroll_x_container_xPos = 65;
var scrolltrack_x:Scrolltrack_x;
var scrollhead_x:Scrollhead_x;
var raster_x_container:Sprite;
var raster_x_container_xPos = 60;
var maskedView_raster_x:Raster_x_Maske;
var raster_y_container:Sprite;
var raster_y_container_yPos = 60;
var maskedView_raster_y:Raster_y_Maske;
var btnArray:ArrayButton;
var mapString:String = "";
//TextFormat
var format:TextFormat = new TextFormat();
format.size = 10;
format.align = TextFormatAlign.CENTER;


//2.UI
//2.1. map: height+width options, generate button
height_mc.height_txt.restrict = "0-9";
width_mc.width_txt.restrict = "0-9";
btnGenerate.addEventListener(MouseEvent.MOUSE_DOWN, generateMap);

//2.2. selectable tiles
selection_container = new Sprite();
selection_container.x = 10;
selection_container.y = 80;
for (var i:int; i=0 && mouseY-scroll_y_container_yPos<=scrolltrack_y.height-scrollhead_y.height+scrollhead_y.height/2) {   
		scrollhead_y.y = mouseY-scroll_y_container_yPos-scrollhead_y.height/2;
	}
	else {
		if (mouseY-scroll_y_container_yPos-scrollhead_y.height/2<0) {
			scrollhead_y.y = 0;
		}
		else {
			scrollhead_y.y = scrolltrack_y.height-scrollhead_y.height;
		}
	}
	if (map_sprite_height <=  map_mask_height) {
		level_container.y = level_container_yPos;
		
		raster_y_container.y = raster_y_container_yPos;
		
	}
	else {
		level_container.y = level_container_yPos-(map_sprite_height-scrolltrack_y.height)*scrollhead_y.y/(scrolltrack_y.height-scrollhead_y.height);
		maskedView_map.y =(map_sprite_height-scrolltrack_y.height)*scrollhead_y.y/(scrolltrack_y.height-scrollhead_y.height);
		
		raster_y_container.y = raster_y_container_yPos-(map_sprite_height-scrolltrack_y.height)*scrollhead_y.y/(scrolltrack_y.height-scrollhead_y.height);
		maskedView_raster_y.y =(map_sprite_height-scrolltrack_y.height)*scrollhead_y.y/(scrolltrack_y.height-scrollhead_y.height);
	}
} 
//3.4.2. scroll map: x-direction
function scroll_map_x_start(event:MouseEvent):void {
	scrollhead_x.removeEventListener(MouseEvent.MOUSE_DOWN, scroll_map_x_start);
	stage.addEventListener(MouseEvent.MOUSE_MOVE, scroll_x); 
	stage.addEventListener(MouseEvent.MOUSE_UP, scroll_map_x_stop);
}
function scroll_map_x_stop(event:MouseEvent):void {
	scrollhead_x.addEventListener(MouseEvent.MOUSE_DOWN, scroll_map_x_start);
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, scroll_x); 
	stage.removeEventListener(MouseEvent.MOUSE_UP, scroll_map_x_stop);
}
function scroll_x(event:MouseEvent):void {
	    if (mouseX-scroll_x_container_xPos-scrollhead_x.width/2>=0 && mouseX-scroll_x_container_xPos<=scrolltrack_x.width-scrollhead_x.width+scrollhead_x.width/2) {   
		scrollhead_x.x = mouseX-scroll_x_container_xPos-scrollhead_x.width/2;
	}
	else {
		if (mouseX-scroll_x_container_xPos-scrollhead_x.width/2<0) {
			scrollhead_x.x = 0;
		}
		else {
			scrollhead_x.x = scrolltrack_x.width-scrollhead_x.width;
		}
	}
	if (map_sprite_width <=  map_mask_width) {
		level_container.x = level_container_xPos;
		
		raster_x_container.x = raster_x_container_xPos;
		
	} 
	else {
		level_container.x = level_container_xPos-(map_sprite_width-scrolltrack_x.width)*scrollhead_x.x/(scrolltrack_x.width-scrollhead_x.width);
		maskedView_map.x =(map_sprite_width-scrolltrack_x.width)*scrollhead_x.x/(scrolltrack_x.width-scrollhead_x.width);
		
		raster_x_container.x = raster_x_container_xPos-(map_sprite_width-scrolltrack_x.width)*scrollhead_x.x/(scrolltrack_x.width-scrollhead_x.width);
		maskedView_raster_x.x =(map_sprite_width-scrolltrack_x.width)*scrollhead_x.x/(scrolltrack_x.width-scrollhead_x.width);
	}
}

//3.5. trace array
function traceArray(evt:MouseEvent):void {
	mapString = "";
	for (var i:int; i

And this is the result:

Download the source code

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers