分块加载(转)
.地圖切割命名,把一張地圖切割成200*200大小小圖,命名0_0.gif這樣的格式。這個簡單不解釋。
2.分塊分析,這里運用的就是數組,假如一張大小6*6地圖,初始化加載0_0–>3_3,這樣16張,而我們只顯示1_1–>2_2這么四張,當顯示2_2—>3_3這樣的四張的時候,就要移除0_0,0_1,0_2,0_3,1_0,2_0,3_0.這樣的幾張,添加4_0,4_1,4_2,4_3,1_4,2_4,3_4.
3.然后把一開始加載的放在一個數組里邊,需要加載的放在另一個里邊,對比一下,需要加載4_0,4_1,4_2,4_3,1_4,2_4,3_4.而沒有加載的加載他,移除0_0,0_1,0_2,0_3,1_0,2_0,3_0。然后齊活。原理就是這個樣子了。
主函數,很水的點擊移動效果:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="400" initialize="init()">
??? <mx:Script>
??????? <![CDATA[
??????????? import com.wind.Maplayer;
??????????? private var mpl:Maplayer;
??????????? private var vx:Number;
??????????? private var vy:Number;
??????????? private var sx:Number;
??????????? private var sy:Number;
??????????? private function init():void{
??????????????? vx = 500;
??????????????? vy = 500;
??????????????? sx = Math.floor(vx/200);
??????????????? sy = Math.floor(vx/200);
??????????????? mpl = new Maplayer();
??????????????? mpl.load(vx,vy);
??????????????? mpl.x = 200-vx;
??????????????? mpl.y = 200-vy;
??????????????? uic.addChild(mpl);
??????????????? addEventListener(Event.ADDED_TO_STAGE,AddedToStageHandler);
??????????? }
??????????? protected function AddedToStageHandler(e:Event):void{
??????????????? this.stage.addEventListener(MouseEvent.CLICK,button1_clickHandler);
??????????????? removeEventListener(Event.ADDED_TO_STAGE,AddedToStageHandler);
??????????? }
??????????? protected function button1_clickHandler(event:MouseEvent):void
??????????? {
??????????????? //trace (mouseX+"=======>"+mouseY);
??????????????? var moux:int = mouseX-200;
??????????????? var mouy:int = mouseY-200;
??????????????? vx +=20;
??????????????? vy +=20;
??????????????? mpl.x = 200-vx;
??????????????? mpl.y = 200-vy;
??????????????? if (!(Math.floor(vx/200)==sx)&&(Math.floor(vy/200)-sy)){
??????????????????? sx=Math.floor(vx/200);
??????????????????? sy=Math.floor(vy/200);
??????????????????? mpl.load(vx,vy);
??????????????????? mpl.x = 200-vx;
??????????????????? mpl.y = 200-vy;
??????????????? }
??????????? }
??????? ]]>
??? </mx:Script>
??? <mx:UIComponent id="uic"/>
</mx:Application>
//修改自網上某些類,可能有些沒必要參數及函數及類包。- -!~
package com.wind{
??? import com.wind.BaseDisplayObject;
??? import flash.display.Bitmap;
??? import flash.events.Event;
??? import flash.events.IOErrorEvent;
??? import flash.events.ProgressEvent;
??? import flash.geom.Point;
??? //地圖層 圖片
??? public class Maplayer extends BaseDisplayObject {
??????? //圖片讀取器
??????? private var _imageLoader:ImageLoader;
??????? //地圖圖片 用于整塊加載模式
??????? private var _image:Bitmap;
??????? //小地圖圖片
??????? private var _simage:Bitmap;
??????? private var _loadType:int;//加載類型 0:整塊加載 1:柵格加載
??????? private var _visualWidth:Number;//地圖可視寬度
??????? private var _visualHeight:Number;//地圖可視高度
??????? private var _sliceWidth:Number;//地圖切割單元寬度
??????? private var _sliceHeight:Number;//地圖切割單元高度
??????? private var _preloadX:Number;//橫向預加載屏數
??????? private var _preloadY:Number;//縱向預加載屏數
??????? private var _loadingMap:HashMap;//正在加載的屏map
??????? private var _imageMap:HashMap;//存放緩存地圖
??????? private var _screenImageRow:int;//一屏需要加載的橫向圖片數
??????? private var _screenImageCol:int;//一屏需要加載的縱向圖片數
??????? private var _row:int;//總橫向節點數
??????? private var _col:int;//總縱向節點數
??????? private var _waitLoadingArr:Array = new Array();
??????? public function Maplayer() {
??????????? _loadingMap = new HashMap();
??????????? _imageMap = new HashMap();
??????? }
??????? public function load(vx:int,vy:int):void {
??????????? _preloadX = 2;
??????????? _preloadY = 2;
??????????? _sliceWidth = 200;//地圖切割單元寬度
??????????? _sliceHeight = 200;
??????????? _row = 18;//總橫向節點數
??????????? _col = 8;//總縱向節點數
??????????? _screenImageRow = 2;
??????????? _screenImageCol = 2;
??????????? var _point:Point = new Point(vx,vy);
??????????? loadSliceImage(_point);
??????????? removeScreenImage(_point)
??????? }
??????? //根據player坐標讀取周邊指定屏數地圖
??????? private function loadSliceImage(playerPoint:Point):void {
??????????? //獲取人物所在塊坐標
??????????? var nowScreenX:int=Math.floor(playerPoint.x/_sliceWidth);
??????????? var nowScreenY:int=Math.floor(playerPoint.y/_sliceHeight);
??????????? //計算顯示范圍坐標
??????????? var startX:int = (nowScreenX - _preloadX < 0 ? 0 : nowScreenX - _preloadX);
??????????? var startY:int = (nowScreenY - _preloadY < 0 ? 0 : nowScreenY - _preloadY);
??????????? var endX:int = (nowScreenX + _preloadX > _row ? _row : nowScreenX + _preloadX);
??????????? var endY:int = (nowScreenY + _preloadY > _col ? _col : nowScreenY + _preloadY);
??????????? //循環加載圖片
??????????? for (var xx:int = startX; xx < endX; xx++) {
??????????????? for (var yy:int = startY; yy < endY; yy++) {
??????????????????? var tempKey:String=xx+"_"+yy;
??????????????????? if(!_loadingMap.containsValue(tempKey) && !_imageMap.containsKey(tempKey)){
??????????????????????? _waitLoadingArr.push(tempKey);
??????????????????? }
??????????????? }
??????????? }
??????????? _waitLoadingArr.reverse();
??????????? //加載圖片
??????????? loadImage();
??????? }
??????? private function loadImage():void {
??????????? //加載圖片類
??????????? if (_waitLoadingArr.length>0) {
??????????????? var arrlen:int = _waitLoadingArr.length;
??????????????? for (var i:int = 0; i<arrlen; i++) {
??????????????????? var key:String=_waitLoadingArr.pop();
??????????????????? var imageLoader:ImageLoader = new ImageLoader();
??????????????????? var fileName:String="bk/images/"+key+".gif";
??????????????????? trace("fileName:" + fileName);
??????????????????? _loadingMap.put(imageLoader,key);
??????????????????? imageLoader.addEventListener(Event.COMPLETE,loadScreenImageSuccess);
??????????????????? imageLoader.addEventListener(ProgressEvent.PROGRESS,loadingHandler);
??????????????????? imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
??????????????????? imageLoader.load(fileName);
??????????????? }
??????????? }
??????? }
??????? //成功加載某屏的圖片
??????? private function loadScreenImageSuccess(evet:Event):void {
??????????? //加載圖片成功處理
??????????? var imageLoader:ImageLoader=ImageLoader(evet.target);
??????????? //求圖片坐標
??????????? var tempStr:String=String(_loadingMap.getValue(imageLoader));
??????????? var tempStrArr:Array=tempStr.split("_");
??????????? var yy:int=tempStrArr[0];
??????????? var xx:int=tempStrArr[1];
??????????? trace (xx+"------->"+yy);
??????????? _loadingMap.remove(imageLoader);
??????????? //加載圖片
??????????? var image:Bitmap=new Bitmap(imageLoader._data);
??????????? image.x=_sliceWidth*xx;
??????????? image.y=_sliceHeight*yy;
??????????? this.addChild(image);
??????????? //存放圖片
??????????? _imageMap.put(yy+"_"+xx,image);
??????????? imageLoader.removeEventListener(Event.COMPLETE,loadScreenImageSuccess);
??????????? imageLoader.removeEventListener(ProgressEvent.PROGRESS,loadingHandler);
??????????? imageLoader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
??????????? imageLoader=null;
??????????? //loadImage();
??????? }
??????? //卸載指定屏的地圖圖片
??????? private function removeScreenImage(playerPoint:Point):void {
??????????? var nowScreenX:int=Math.floor(playerPoint.x/_sliceWidth);
??????????? var nowScreenY:int=Math.floor(playerPoint.y/_sliceHeight);
??????????? var startX:int = (nowScreenX - _preloadX < 0 ? 0 : nowScreenX - _preloadX);
??????????? var startY:int = (nowScreenY - _preloadY < 0 ? 0 : nowScreenY - _preloadY);
??????????? var endX:int = (nowScreenX + _preloadX > _row ? _row : nowScreenX + _preloadX);
??????????? var endY:int = (nowScreenY + _preloadY > _col ? _col : nowScreenY + _preloadY);
??????????? ///獲取圖片數組
??????????? var keyArr:Array=_imageMap.keys();
??????????? for (var i:int = 0; i < keyArr.length; i++) {
??????????????? //刪掉超出范圍不需要顯示的圖片
??????????????? var key:String=keyArr[i];
??????????????? var tempStrArr:Array=key.split("_");
??????????????? var yy:int=tempStrArr[0];
??????????????? var xx:int=tempStrArr[1];
??????????????? if (xx<startX||xx>endX||yy<startY||yy>endY) {
??????????????????? var image:Bitmap=Bitmap(_imageMap.getValue(key));
??????????????????? this.removeChild(image);
??????????????????? image=null;
??????????????????? _imageMap.remove(key);
??????????????? }
??????????? }
??????????? //強制垃圾回收
??????????? HeptaFishGC.gc();
??????? }
??? }
}
HashMap
package com.wind
{
??? import flash.utils.Dictionary;
??? public class HashMap
??? {
??????? private var _keys:Array=null;
??????? private var props:Dictionary=null;
??????? public function HashMap() {
??????????? this.clear();
??????? }
??????? public function clear():void {
??????????? this.props=new Dictionary? ;
??????????? this._keys=new Array? ;
??????? }
??????? public function containsKey(key:Object):Boolean {
??????????? return this.props[key]!=null;
??????? }
??????? public function containsValue(value:Object):Boolean {
??????????? var result:Boolean=false;
??????????? var len:uint=this.size();
??????????? if (len>0) {
??????????????? for (var i:uint=0; i<len; i++) {
??????????????????? if (this.props[this._keys[i]]==value) {
??????????????????????? result =? true;
??????????????????????? break;
??????????????????? }
??????????????? }
??????????? }
??????????? return result;
??????? }
??????? public function getValue(key:Object):Object {
??????????? return this.props[key];
??????? }
??????? public function put(key:Object,value:Object):Object {
??????????? var result:Object=null;
??????????? if (this.containsKey(key)) {
??????????????? result=this.getValue(key);
??????????????? this.props[key]=value;
??????????? } else {
??????????????? this.props[key]=value;
??????????????? this._keys.push(key);
??????????? }
??????????? return result;
??????? }
??????? public function remove(key:Object):Object {
??????????? var result:Object=null;
??????????? if (this.containsKey(key)) {
??????????????? delete this.props[key];
??????????????? var index:int=this._keys.indexOf(key);
??????????????? if (index>-1) {
??????????????????? this._keys.splice(index,1);
??????????????? }
??????????? }
??????????? return result;
??????? }
??????? public function putAll(map:HashMap):void {
??????????? this.clear();
??????????? var len:uint=map.size();
??????????? if (len>0) {
??????????????? var arr:Array=map.keys();
??????????????? for (var i:uint=0; i<len; i++) {
??????????????????? this.put(arr[i],map.getValue(arr[i]));
??????????????? }
??????????? }
??????? }
??????? public function size():uint {
??????????? return this._keys.length;
??????? }
??????? public function isEmpty():Boolean {
??????????? return this.size()? <1;
??????? }
??????? public function values():Array {
??????????? var result:Array=new Array? ;
??????????? var len:uint=this.size();
??????????? if (len>0) {
??????????????? for (var i:uint=0; i<len; i++) {
??????????????????? result.push(this.props[this._keys[i]]);
??????????????? }
??????????? }
??????????? return result;
??????? }
??????? public function keys():Array {
??????????? return this._keys;
??????? }
??? }
}
ImageLoader
package com.wind
{
??? import flash.display.BitmapData;
??? import flash.events.Event;
??? import flash.system.LoaderContext;
??? import flash.utils.ByteArray;
??? public class ImageLoader extends BaseLoader{
??????? public var _data:BitmapData;
??????? //構造函數
??????? public function ImageLoader(obj:Object = null,lc:LoaderContext = null) {
??????????? //_type = HeptaFishConstant.TYPE_LOADER_IMAGELOADER;
??????????? if(obj != null){
??????????????? if(obj is ByteArray){
??????????????????? loadBytes(obj as ByteArray,lc);
??????????????? }else if(obj is String){
??????????????????? load(obj as String,lc);
??????????????? }else{
??????????????????? throw new Error("參數錯誤,構造函數第一參數只接受ByteArray或String");
??????????????? }
??????????? }
??????? }
??????? //加載成功,發布成功事件
??????? override protected function completeFun(e:Event):void {
??????????? _data = _loader.content["bitmapData"];
??????????? super.completeFun(e);
??????? }
??????? //清除
??????? override public function clear():void{
??????????? _data = null;
??????????? super.clear();
??????? }
??? }
}
HeptaFishGC
package com.wind
{
??? import flash.net.LocalConnection;
??? public class HeptaFishGC
??? {
??????? public function HeptaFishGC()
??????? {
??????? }
??????? public static function gc():void{
??????????? try{
??????????????? new LocalConnection().connect("foo");
??????????????? new LocalConnection().connect("foo");
??????????? }catch(e:Error){
??????????? }
??????? }
??? }
}
BaseLoader
package com.wind
{
??? import flash.display.Loader;
??? import flash.events.Event;
??? import flash.events.EventDispatcher;
??? import flash.events.IOErrorEvent;
??? import flash.events.ProgressEvent;
??? import flash.net.URLRequest;
??? import flash.system.LoaderContext;
??? import flash.utils.ByteArray;
??? public class BaseLoader extends EventDispatcher
??? {
??????? protected var _url:String;
??????? protected var _loader:Loader;
??????? protected var _name:String;
??????? protected var _type:String;
??????? public function BaseLoader()
??????? {
??????? }
??????? //加載
??????? public function load(url:String,lc:LoaderContext = null):void{
??????????? _url = url;
//??????????? trace(_url);
??????????? _loader = new Loader();
??????????? _loader.load(new URLRequest(url),lc);
??????????? addEvent();
??????? }
??????? //加載字節
??????? public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void{
??????????? _loader = new Loader;
??????????? _loader.loadBytes(bytes,lc);
??????????? addEvent();
??????? }
??????? //開始偵聽
??????? protected function addEvent():void{
??????????? _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun);
??????????? _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun);
??????????? _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorFun);
??????? }
??????? //結束偵聽
??????? protected function delEvent():void{
??????????? _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFun);
??????????? _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFun);
??????????? _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorFun);
??????? }
???????? //加載成功,發布成功事件
??????? protected function completeFun(e:Event):void {
??????????? dispatchEvent(e);
??????????? delEvent();
??????? }
??????? //加載過程
??????? protected function progressFun(e:ProgressEvent):void{
??????????? dispatchEvent(e);
??????? }
??????? //錯誤處理
??????? protected function ioErrorFun(e:Event):void{
??????????? trace("讀取文件出錯,未找到文件!");
??????????? //Alert.show("讀取文件出錯,未找到文件!");
??????? }
??????? //清除
??????? public function clear():void{
??????????? _loader.unload();
??????????? _loader = null;
??????????? HeptaFishGC.gc();
??????? }
??????? public function get url():String{
??????????? return _url;
??????? }
??????? public function set url(url:String):void{
??????????? _url = url;
??????? }
??????? public function get name():String{
??????????? return _name;
??????? }
??????? public function set name(name:String):void{
???????????? _name = name;
??????? }
??????? public function get loader():Loader{
??????????? return _loader;
??????? }
??????? public function get type():String{
??????????? return _type;
??????? }
??? }
}
BaseDisplayObject
package com.wind
{
??? import flash.display.DisplayObject;
??? import flash.display.DisplayObjectContainer;
??? import flash.display.Sprite;
??? import flash.events.Event;
??? import flash.events.IOErrorEvent;
??? import flash.events.ProgressEvent;
??? import flash.geom.Rectangle;
??? public class BaseDisplayObject extends Sprite
??? {
??????? public function BaseDisplayObject()
??????? {
??????????? super();
??????????? addEventListener(Event.REMOVED,removedFun);
??????? }
??????? override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void
??????? {
??????????? super.addEventListener(type, listener, useCapture, priority, useWeakReference);
??????? }
??????? public function get measuredMinWidth():Number
??????? {
??????????? return 0;
??????? }
??????? override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
??????? {
??????????? super.removeEventListener(type, listener, useCapture);
??????? }
??????? override public function dispatchEvent(event:Event):Boolean
??????? {
??????????? return super.dispatchEvent(event);
??????? }
??????? public function initialize():void
??????? {
??????? }
??????? protected function removedFun(evet:Event):void{
??????????? HeptaFishGC.gc();
??????? }
??????? protected function dispatch(eventClass:Class,eventType:String,par:Object = null):void{
??????????? var event:Event = new eventClass(eventType,par);
??????????? this.dispatchEvent(event);
??????? }
??????? protected function loadingHandler(evet:ProgressEvent):void{
??????????? dispatchEvent(evet);
??????? }
??????? protected function ioErrorHandler(evet:IOErrorEvent):void{
??????????? dispatchEvent(evet);
??????? }
??? }
}
轉載于:https://www.cnblogs.com/axyz/archive/2011/11/25/2263231.html
總結
- 上一篇: API Gateway——KONG简单入
- 下一篇: 【记录】一些平时留意过的