`

mapeidt简单解析类

    博客分类:
  • flex
阅读更多
<map filename="E:\Program Files\mapedit2\main\images\maps\1.jpg" name="test2" mapwidth="300" mapheight="300" loadType="0" roadType="0">
  <floor tileWidth="60" tileHeight="30" row="20" col="5">
  1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
  0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,
  0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1
  
  
  </floor>
  <items>
    <item id="2" file="障碍/靠背椅1.png" type="building" xoffset="60" yoffset="30" px="90" py="105" ix="2" iy="11">
      <walkable>120,90</walkable>
    </item>
    <item id="3" file="障碍/花坛1.png" type="building" xoffset="60" yoffset="30" px="180" py="180" ix="3" iy="13">
      <walkable>90,45,90,75</walkable>
    </item>
  </items>
</map>

 

package main
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	public class MainScene extends UIComponent
	{
		private var mapxml:XML
		private var mapArray:Array=[];
		private var col:int;
		private var row:int;
		public function MainScene(mapxml:XML)
		{
			this.mapxml=mapxml;
			this.col=mapxml.floor.@col;
			this.row=mapxml.floor.@row;
			Alert.show("col"+col);
			Alert.show("row"+row);
		}
		public function init():void{
			var loader:Loader=new Loader();
			loader.load(new URLRequest(mapxml.@filename));
			//loader.addEventListener(Event.COMPLETE,onmapCommit);
			this.addChild(loader);//背景
			initbuilder();// 建筑层
			initArray();//floor
		}
		//加载地图完成
		private function onmapCommit(event:Event):void{
			this.addChild(event.target as DisplayObject);
			initbuilder();//初始化建筑物
			
		}
		//初始化建筑层
		private function initbuilder(){
			Alert.show(mapxml.items.item);
			for each(var build:XML in mapxml.items.item){
				var loader:Loader=new Loader();
				loader.load(new URLRequest(build.@file));
				this.addChild(loader);
				loader.x=build.@px;
				loader.y=build.@py;
			}
			
		}
		//一行一行取,
		private function initArray(){
			var zero:String=mapxml.floor;
			var zeroArr:Array=zero.split(",");
			for(var i:int=0;i<col;i++){
				mapArray[i]=new Array
				for(var j:int=0;j<row;j++){
					mapArray[i][j]=zeroArr[j+row*i];//
					//trace("i==="+i+"j=="+j);
				}
				
			}
			trace(mapArray[4]);
//			Alert.show(zero);
		}
		
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	
	<mx:Script>
		<![CDATA[
			import main.MainScene;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			private var mapxml:XML;
			private var mains:MainScene;
			private function init(){
				var loader:URLLoader=new URLLoader();
				loader.load(new URLRequest("map/test2.xml"));
				loader.addEventListener(Event.COMPLETE,resultHandle)
				
			}
			private function resultHandle(event:Event){
				var mapxml:XML=new XML(event.target.data);
				mains=new MainScene(mapxml);
				mains.init();
				this.addChild(mains);
				
			}
		]]>
	</mx:Script>
</mx:Application>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics