flash地图拼接

/*
*  文件名:地图拼接
*  创建人:Clark/陈泽丹
*  创建时间: 20120306
*/



package Src.Tool
{
	import Src.Tool.Imager;
	import flash.geom.Point;
	import flash.display.BitmapData;

	public class FileImgers
	{
		private var m_Imager:Array = null;
		private var W_SIZE:int = 0;
		private var H_SIZE:int = 0;
		private const UnitW:int = 800;
		private const UnitH:int = 600;
		private const SceneW:int = 800;
		private const SceneH:int = 600;
		private const W_NUM_LIMIT:int = SceneW/UnitW +1;
		private const H_NUM_LIMIT:int = SceneH/UnitH +1;
		public function FileImgers(_szFilePath:String, _iWSize:int, _iHSize:int)
		{
			// constructor code
			W_SIZE = _iWSize;
			H_SIZE = _iHSize;
			m_Imager = new Array();
			const iSize:int = _iWSize*_iHSize;
			for(var i:int = 0; i<iSize; i = i+1)
			{
				m_Imager[i] = new Imager(_szFilePath + "\\" + i + ".jpg");
			}
		}
		public function Delete()
		{
			for(var i:int = 0; i<m_Imager.length; i++)
			{
				m_Imager[i].Delete();
				m_Imager[i] = null;
			}
			m_Imager = null;
		}
		public function drawScene(_destImg:BitmapData, _iWorldX:int, _iWorldY:int)
		{
			var ImgID:Point = WorldPos2ImgID(_iWorldX, _iWorldY);
			var vImgIDs:Array = getSceneImgIDs(ImgID.x, ImgID.y);
			for(var i:int = 0; i<vImgIDs.length; i++)
			{
				var pt:Point = getImgWorldPos(vImgIDs[i])
				var ptImg:Point = new Point(pt.x-_iWorldX, pt.y - _iWorldY)
				m_Imager[vImgIDs[i]].drawImg(_destImg, ptImg);
			}
		}
		private function WorldPos2ImgID(_iWorldX:int, _iWorldY:int):Point
		{
			var ImgIDX:int = _iWorldX/UnitW;
			var ImgIDY:int = _iWorldY/UnitH;
			return new Point(ImgIDX, ImgIDY)
		}
		private function getImgWorldPos(_iImgID:int):Point
		{
			var iImgIDx:int = _iImgID%W_SIZE;
			var iImgIDy:int = _iImgID/W_SIZE;
			return new Point(iImgIDx*UnitW, iImgIDy*UnitH);
		}
		private function getSceneImgIDs(_iStartImgIDX:int, _iStartImgIDY:int):Array
		{
			var pIDs:Array = new Array()
			for(var i:int = 0; i<H_NUM_LIMIT; i++)
			{
				for(var j:int = 0; j<W_NUM_LIMIT; j++)
				{
					var wTmp:int = _iStartImgIDX + j;
					var hTmp:int = _iStartImgIDY + i;
					if( wTmp < W_SIZE && hTmp < H_SIZE)
					{
						var vTmp:int = hTmp * W_SIZE + wTmp;
						if( vTmp >= 0 && vTmp<W_SIZE*H_SIZE)
						{
							pIDs[pIDs.length] = vTmp
						}
					}
				}
			}
			return pIDs;
		}
	}
}


 

package Src.Scene.MainScene
{
	import flash.system.System;
	import Src.Main;
	import Src.Tool.Swfer;
	import Src.Tool.Imager;
	import Src.Tool.FileImgers;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class MainScene
	{
		private var m_Swfer:Swfer = null;
		private var m_SceneImg:Imager = null;
		private var m_FileImgs:FileImgers = null;
		private var m_WorldPos:Point = null;
		private var m_rectScene:Rectangle = null;
		public static function CreateObj()
		{
			return new MainScene();
		}
		public function MainScene()
		{
			// constructor code
			m_Swfer = new Swfer("Src\\Res\\MainSceneRes\\boat.swf",InitBoat);
			m_SceneImg = new Imager();
			m_FileImgs = new FileImgers("Src\\Res\\MainSceneRes\\BK",4,4);
			m_WorldPos = new Point(1600,1600);
			m_rectScene = new Rectangle(0,0,800,600);
			Main.g_MainScene.addEventListener(MouseEvent.CLICK, Click);
		}
		public function Delete()
		{
			Main.g_MainScene.removeEventListener(MouseEvent.CLICK, Click);
			m_Swfer.Delete();
			m_Swfer = null;
			m_SceneImg.Delete()
			m_SceneImg = null;
			m_FileImgs.Delete();
			m_FileImgs = null;
			m_WorldPos = null;
			m_rectScene = null;
		}
		public function Click(evt:MouseEvent)
		{
			var xTmp:int = evt.localX - m_Swfer.m_Swf.x;
			var yTmp:int = evt.localY - m_Swfer.m_Swf.y;
			m_WorldPos.x = m_WorldPos.x + xTmp;
			m_WorldPos.y = m_WorldPos.y + yTmp;
			//trace(evt.localX, evt.localY);
		}
		public function InitBoat()
		{
			m_Swfer.m_Swf.x = 350;
			m_Swfer.m_Swf.y = 300;
		}
		public function drawScene()
		{
			Main.g_MainScene.graphics.beginFill(0x000000);
			const x:int = flash.system.Capabilities.screenResolutionX;
			const y:int = flash.system.Capabilities.screenResolutionY;
			Main.g_MainScene.graphics.drawRect(0, 0, x, y);
			Main.g_MainScene.graphics.endFill();
			m_SceneImg.m_Img.bitmapData.fillRect(m_rectScene,0x000000);
			m_FileImgs.drawScene(m_SceneImg.m_Img.bitmapData, m_WorldPos.x,m_WorldPos.y);
			Main.g_MainScene.addChild(m_SceneImg.m_Img);
			m_Swfer.drawSwf(Main.g_MainScene);
		}
	}
}


 

package Src.Tool
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	public class Swfer extends Sprite
	{
		private var m_Loader:Loader = null;
		private var m_InitFun:Function = null;
		private var m_szUrl:String = null;
		public var m_Swf:MovieClip = null;
		public function Swfer(_szUrl:String, _funInit:Function = null)
		{
			// constructor code
			m_szUrl = _szUrl
			m_InitFun = _funInit;
			m_Loader = new Loader();
			m_Loader.load(new URLRequest(m_szUrl));
			m_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
		}
		public function Delete()
		{
			m_Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			m_Loader = null;
			m_InitFun = null;
			m_szUrl = null;
			m_Swf = null;
		}
		private function onComplete(_evt:Event):void  
        {  
            m_Swf = _evt.target.content as MovieClip;  
			if( null == m_Swf)
			{
			 	trace("下载" + m_szUrl + "文件失败!");
			}
			m_Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			m_Loader = null;
			if( null != m_InitFun)
			{
				m_InitFun();
				m_InitFun = null;
			}
        }
		public function drawSwf(_Scene:Sprite)
		{
			if( null != m_Swf)
			{
				_Scene.addChild(m_Swf);
			}
		}
	}
}


 

package Src.Tool
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	public class Imager
	{
		private var m_Loader:Loader = null;
		private var m_InitFun:Function = null;
		private var m_szUrl:String = null;
		public var m_Img:Bitmap = null;
		public function Imager(_szUrl:String = null, _funInit:Function = null)
		{
			// constructor code
			m_szUrl = _szUrl;
			m_InitFun = _funInit;
			if (null == m_szUrl)
			{
				var pBD:BitmapData = new BitmapData(800,600,true,0x000000);
				m_Img = new Bitmap(pBD);
				if (null != m_InitFun)
				{
					m_InitFun();
					m_InitFun = null;
				}
			}
			else
			{
				m_Loader = new Loader();
				m_Loader.load(new URLRequest(m_szUrl));
				m_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			}
		}
		public function Delete()
		{
			m_Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			m_Loader = null;
			m_InitFun = null;
			m_szUrl = null;
			m_Img = null;
		}
		private function onComplete(_evt:Event):void
		{
			m_Img = Bitmap(_evt.target.content);
			if (null == m_Img)
			{
				trace("下载" + m_szUrl + "文件失败!");
			}
			m_Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			m_Loader = null;
			if (null != m_InitFun)
			{
				m_InitFun();
				m_InitFun = null;
			}
		}
		public function drawImg(_destImg:BitmapData, _destPos:Point, _srcRect:Rectangle = null)
		{
			if (null != m_Img)
			{
				if (null == _srcRect)
				{
					_srcRect = new Rectangle(0,0,m_Img.bitmapData.width,m_Img.bitmapData.height);
				}
				_destImg.copyPixels(m_Img.bitmapData, _srcRect, _destPos);
			}
		}
	}
}


 

package Src
{

	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	public class Main extends Sprite
	{
		private const m_RefreshTimer:Timer = new Timer(100,0);
		private var m_SceneItems:SceneItems = null;
		public 	static const g_MainScene:Sprite = new Sprite();
		public function Main():void
		{
			// constructor code
			this.graphics.clear();
			this.addChild(g_MainScene);
			m_SceneItems = new SceneItems("MainScene");
			m_RefreshTimer.addEventListener(TimerEvent.TIMER, onDrawTime);
			m_RefreshTimer.start();
		}
		public function onDrawTime(evt:TimerEvent)
		{
			m_SceneItems.drawScene();
		}
	}
}

import Src.Main;
import Src.Scene.MainScene.MainScene
class SceneItems
{
	private const g_SceneItems:Object = new Object();
	private var g_CurItem:Object = null;
	public function SceneItems(_szCurScene:String):void
	{
		// constructor code
		g_SceneItems["MainScene"] = MainScene.CreateObj;
		selectScene(_szCurScene);
	}
	public function selectScene(_szCurScene:String)
	{
		if (null != g_SceneItems[_szCurScene])
		{
			trace("OK,访问场景 " + _szCurScene + " !");
			clearScene();
			if( null != g_CurItem)
			{
				g_CurItem.Delete();
			}
			g_CurItem = g_SceneItems[_szCurScene]();
		}
		else
		{
			trace("Error,访问空场景 " + _szCurScene + " !");
		}
	}
	public function clearScene()
	{
		//释放资源
		g_CurItem = null;
		//清空屏幕
		for (var i:int=Main.g_MainScene.numChildren-1; i>=0; i--)
		{
			Main.g_MainScene.removeChildAt(0);
		}
		Main.g_MainScene.graphics.clear();
	}
	public function drawScene()
	{
		if( null != g_CurItem)
		{
			g_CurItem.drawScene();
		}
	}
}


 

你可能感兴趣的:(flash地图拼接)