WEB开发网
开发学院图形图像Flash AS3 level editor 阅读

AS3 level editor

 2009-10-18 00:00:00 来源:WEB开发网   
核心提示: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 ed

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<totalTiles; i++) {
 t = new Tile();
 t.gotoAndStop(i + 1);
 selection_container.addChild(t);
 t.y = (tile_size) * i;
 t.addEventListener(MouseEvent.MOUSE_DOWN, selectionClick);
}
var maskedView:Maske = new Maske();
selection_container.addChild(maskedView);
selection_container.mask=maskedView;
addChild(selection_container);
 
 
//3. functions
//3.1. selectable tiles
//3.1.1. current tile
function selectionClick (event:MouseEvent):void {
 currentTile = event.currentTarget.currentFrame;
 currentTile_mc.gotoAndStop(currentTile);
}
//3.1.2 scrolling
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel); 
var scroll_speed = 10;
var scroll_speed_multiplikator = 8;
function onMouseWheel (event:MouseEvent):void {
   if(event.delta < 0){
      //trace ("MouseWheel down");
   selection_container.y += scroll_speed;
   maskedView.y -= scroll_speed;
 } 
 else {
      //trace ("MouseWheel up");
   selection_container.y -= scroll_speed;
   maskedView.y += scroll_speed;
   }
}
up.addEventListener(MouseEvent.MOUSE_DOWN, scroll_up);
function scroll_up(event:MouseEvent):void {
 selection_container.y -= scroll_speed*scroll_speed_multiplikator;
 maskedView.y += scroll_speed*scroll_speed_multiplikator;
}
down.addEventListener(MouseEvent.MOUSE_DOWN, scroll_down);
function scroll_down(event:MouseEvent):void {
 selection_container.y += scroll_speed*scroll_speed_multiplikator;
 maskedView.y -= scroll_speed*scroll_speed_multiplikator;
} 
reset.addEventListener(MouseEvent.MOUSE_DOWN, scroll_start);
function scroll_start(event:MouseEvent):void {
 selection_container.y = 80;
 maskedView.y = 0;
}
 
//3.2. generate button
//3.2.1. on Enter, too
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
function key_down(event:KeyboardEvent) {
 if (event.keyCode == 13) { //Enter?
  generateMap(); 
 }
}
//3.2.2. generate map
//3.2.2.1. get width, height
function generateMap(evt:MouseEvent = null):void {
 map_height =  height_mc.height_txt.text;
 map_width =  width_mc.width_txt.text;
//3.2.2.2. place containers
 if (level_container) {
  removeChild(level_container);
  removeChild(raster_x_container);
  removeChild(raster_y_container);
  removeChild(scroll_y_container);
  removeChild(scroll_x_container);
  removeChild(btnArray);
  map = []; 
    } 
 scroll_x_container = new Sprite ();
 scroll_x_container.x = scroll_x_container_xPos;
 scroll_x_container.y = 415;
 addChild(scroll_x_container);
 scroll_y_container = new Sprite ();
 scroll_y_container.x = 480;
 scroll_y_container.y = scroll_y_container_yPos;
 addChild(scroll_y_container);
 raster_x_container = new Sprite ();
 raster_x_container.x = raster_x_container_xPos;
 raster_x_container.y = 40;
 addChild(raster_x_container); 
 raster_y_container = new Sprite ();
 raster_y_container.x = 50;
 raster_y_container.y = raster_y_container_yPos;
 addChild(raster_y_container); 
 level_container = new Sprite(); 
 level_container.x = level_container_xPos;
 level_container.y = level_container_yPos;
 addChild(level_container);       
//3.2.2.3. add tiles, map array 
 for (var a=0; a<map_height; a++) {    //rows
  var raster_y:TextField = new TextField ();
  raster_y.defaultTextFormat = format;
  raster_y.text = ""+a;
  raster_y_container.addChild(raster_y);
  raster_y.x = 0-48;
  raster_y.y = a*tile_size+2;
  raster_y.selectable = false;
  map[a] = new Array();      
  for (var b=0; b<map_width; b++) {    //columns
   if (a==0) {
    var raster_x:TextField = new TextField ();
    raster_x.defaultTextFormat = format;
    raster_x.text = ""+b;
    raster_x_container.addChild(raster_x);
    raster_x.x = b*tile_size+10-46;
    raster_x.y = 0;
    raster_x.selectable = false;
   }
   t = new Tile();    
   t.x = b*tile_size;      
   t.y = a*tile_size;      
   t.gotoAndStop(0);     
   map[a][b] = t.currentFrame-1;      
   level_container.addChild(t);   
   t.addEventListener(MouseEvent.MOUSE_DOWN, editClick);
  }
 }
//3.2.2.4. add scrollbars
 //y-Scrollbar
 map_sprite_height = map_height*tile_size;
 scrolltrack_y = new Scrolltrack_y ();
 scroll_y_container.addChild(scrolltrack_y);
 scrollhead_y = new Scrollhead_y ();
 scroll_y_container.addChild(scrollhead_y);
 if (map_sprite_height <=  map_mask_height) {
  scrollhead_y.height = map_mask_height;
 }
 else {
  scrollhead_y.height = map_mask_height/map_sprite_height*map_mask_height; 
 }
 scrollhead_y.addEventListener(MouseEvent.MOUSE_DOWN, scroll_map_y_start);
 scrollhead_y.addEventListener(MouseEvent.MOUSE_UP, scroll_map_y_stop);
 //x-Scrollbar
 map_sprite_width = map_width*tile_size;
 scrolltrack_x = new Scrolltrack_x ();
 scroll_x_container.addChild(scrolltrack_x);
 scrollhead_x = new Scrollhead_x ();
 scroll_x_container.addChild(scrollhead_x);
 if (map_sprite_width <=  map_mask_width) {
  scrollhead_x.width = map_mask_width;
 }
 else {
  scrollhead_x.width = map_mask_width/map_sprite_width*map_mask_width; 
 }
 scrollhead_x.addEventListener(MouseEvent.MOUSE_DOWN, scroll_map_x_start);
 scrollhead_x.addEventListener(MouseEvent.MOUSE_UP, scroll_map_x_stop);
//3.2.2.5. add masks
 //mask for map
 maskedView_map = new Map_Maske();
 level_container.addChild(maskedView_map);
 level_container.mask=maskedView_map;
 //mask for x-scale
 maskedView_raster_x = new Raster_x_Maske ();
 raster_x_container.addChild(maskedView_raster_x);
 raster_x_container.mask=maskedView_raster_x;
 //mask for y-scale
 maskedView_raster_y = new Raster_y_Maske ();
 maskedView_raster_y.x = -5;
 raster_y_container.addChild(maskedView_raster_y);
 raster_y_container.mask=maskedView_raster_y;
//3.2.2.6. add btnArray
 btnArray = new ArrayButton ();
 btnArray.x = 390;
 btnArray.y = 7;
 addChild(btnArray);
 btnArray.addEventListener(MouseEvent.MOUSE_DOWN, traceArray);
}
 
//3.3. edit map
function editClick (event:MouseEvent):void {
 event.currentTarget.gotoAndStop(currentTile);
 var mouse_x = Math.floor((mouseX-level_container.x)/tile_size);
 var mouse_y = Math.floor((mouseY-level_container.y)/tile_size);
 map[mouse_y][mouse_x] = event.currentTarget.currentFrame-1;
}
 
//3.4. scroll map
//3.4.1. scroll map: y-direction
function scroll_map_y_start(event:MouseEvent):void {
 scrollhead_y.removeEventListener(MouseEvent.MOUSE_DOWN, scroll_map_y_start);
 stage.addEventListener(MouseEvent.MOUSE_MOVE, scroll_y); 
 stage.addEventListener(MouseEvent.MOUSE_UP, scroll_map_y_stop);
}
function scroll_map_y_stop(event:MouseEvent):void {
 scrollhead_y.addEventListener(MouseEvent.MOUSE_DOWN, scroll_map_y_start);
 stage.removeEventListener(MouseEvent.MOUSE_MOVE, scroll_y); 
 stage.removeEventListener(MouseEvent.MOUSE_UP, scroll_map_y_stop);
}
function scroll_y(event:MouseEvent):void {
    if (mouseY-scroll_y_container_yPos-scrollhead_y.height/2>=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<map_height; i++) { //rows
  mapString += "map["+i+"]=[";
  for (var j:int; j<map_width; j++) {  //columns
   mapString += map[i][j];
   if (j<map_width-1) {
    mapString += ", ";
   }
  }
  mapString += "]; \n"; 
  j = 0;
 }
 trace (mapString);
}

1 2  下一页

Tags:AS level editor

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接