zoukankan      html  css  js  c++  java
  • 分块加载(转)

    .地图切割命名,把一张地图切割成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);

            }

        }

    }

  • 相关阅读:
    《不生不熟》读后感 读书笔记
    《亚洲与一战》读后感 读书笔记
    《厨房》读后感 读书笔记
    《娇惯的心灵》读后感 读书笔记
    《实践理性批判》读后感 读书笔记
    嵌入式三级知识点整理
    C语言:输入一个数,输出比这个数小的所有素数,并求出个数。
    C语言知识点记录
    C语言-实现矩阵的转置-随机函数产生随机数并赋予数组中-190222
    将数字字符转为数字的两种方法。
  • 原文地址:https://www.cnblogs.com/axyz/p/2263231.html
Copyright © 2011-2022 走看看