1,在一些项目里面可能要缓存图片,这样有助于面板的快速的展现:

如是,就出现了2中情况:

①:刚开始加载游戏时,就顺带着去分布式加载图片(所有重要的)

②:在需要的时候加载,并存入缓存,下次需要时,直接读缓存

这2种方案都有利弊:

第一种,会拖延加载进度,可能会导致内存,性能的损耗

第二种,第一次加载时,面板的显示速度慢

我现在的方案是:

2种方式看玩家信息情况决定:

图片加载管理器类:


package com.upupgame.mahjong.player2.hall.views.ui.controls.tasks{
    import com.upupgame.mahjong.player2.common.biz.LoadingBiz;
    import com.upupgame.mahjong.player2.hall.models.tasks.TaskDataModel;
    import com.upupgame.mahjong.player2.hall.models.tasks.TasksItemsIconsDataModel;
                                       
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    /**
     * 任务元素图标管理</br>
     * 创建时间  31115.1
     * @author Kayer
     * */
    public class TasksItemsIconsManager{
//      taskIcon = LoadingBiz.CLIENTURL+"res/task/"+ary.type+".png";
        private static var $instance : TasksItemsIconsManager;
        public static function get instance() : TasksItemsIconsManager{
            if( $instance == null ) $instance = new TasksItemsIconsManager();
            return $instance;
        }
        public function TasksItemsIconsManager(){
            if($instance != null){
                throw new Error("TasksItemsIconsManager 被设计成为了单例");
            }else{
                $instance = this;
            }
            $iconDataModelsVec = new Vector.<TasksItemsIconsDataModel>();
        }
        private var $iconDataModelsVec : Vector.<TasksItemsIconsDataModel>;
//      private var $nowWorking : Boolean = false;
        private var $loadIndex : int = 0;
        private var $rootData : Vector.<String >;//存放 Type
        private var $len : uint = 0;
        /**
         * 初始化数据
         * */
        public function initData( $data : Vector.<String > ) : void{
            if($data != null && $data.length > 0){
                this.$rootData = $data;
                this.$len = $data.length;
                createNewIconByPath("pass");
            }
        }
        /**
         * 得到 一个 BitMap 类
         * */
        public function getIconByPath( $iconPath : String , $callBack : Function = null ) : void{
            if($iconDataModelsVec != null && $iconDataModelsVec.length > 0){
                for each( var $model : TasksItemsIconsDataModel in $iconDataModelsVec){
                    if($model.iconPath == $iconPath){
                        if($callBack != null ){
                            trace("31115.1---我取得缓存--ICON");
                            $callBack($model.iconBitMap,"");
                            Debug.log("31115.1---我取得缓存--ICON");
                        }
                        return;
                    }
                }
                loaderIcon($iconPath,$callBack);
            }else{
                loaderIcon($iconPath,$callBack);
            }
        }
        private function createNewIconByPath( $path : String = "pass") : void{
            if($loadIndex < $len && $rootData != null && $path == "pass"){
                trace("31115.1我在加载 ---分布式---");
                loaderIcon(LoadingBiz.CLIENTURL+"res/task/"+$rootData[$loadIndex]+".png");//自己组建 URL
                Debug.log("31115.1我在加载 ---分布式---");
            }else{
                if($path != "pass"){
                    loaderIcon($path);
                }
            }
        }
        private function loaderIcon( $path : String , $callBack : Function = null ) : void{
            var $myLoader : Loader = new Loader();
            if($callBack == null){
                $myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,__onLoaded,false,0,true);
                $myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,__onLoadingError,false,0,true);
                $myLoader.load(new URLRequest($path),new LoaderContext(true));
            }else{
                Debug.log("31115.1---自我加载");
                $callBack($myLoader,$path);
            }
        }
        private function __onLoaded( $e : Event ) : void{
            var $myLoaderInfo : LoaderInfo = $e.target as LoaderInfo;
            if ($myLoaderInfo != null && $myLoaderInfo.loader != null && $myLoaderInfo.loader.content && $myLoaderInfo.loader.content is Bitmap) {
                var $bm : Bitmap = $myLoaderInfo.loader.content as Bitmap;
                if($bm != null){
                    var $bufferIcon : TasksItemsIconsDataModel = new TasksItemsIconsDataModel();
                    $bufferIcon.iconPath = LoadingBiz.CLIENTURL+"res/task/"+$rootData[$loadIndex]+".png";
                    $bufferIcon.iconBitMap = $bm;
                    $iconDataModelsVec.push($bufferIcon); //加入到缓存
                }
                $myLoaderInfo.loader.unload();
            }
            if($rootData != null ){
                $loadIndex += 1;
                if($loadIndex >= $len){
                    $rootData = null;
                }else{
                    createNewIconByPath("pass");
                }
            }
        }
        private function __onLoadingError( $e : IOErrorEvent ) : void{
            if($rootData != null){
                $loadIndex += 1;
                if($loadIndex >= $len){
                    $rootData = null;
                }else{
                    createNewIconByPath("pass");
                }
            }
        }
        /**
         * 增加一个新的图标模型
         * */
        public function addNewModel( $taskIconModel : TasksItemsIconsDataModel ) : void{
            if( null != $taskIconModel ){
                this.$iconDataModelsVec.push($taskIconModel);
            }
        }
    }
}
如,玩家有任务需要领取,(刚刚进入游戏时,请求后端是)

顺带要请求ICONs

var $modelsV : Vector.<String> = new Vector.<String>();
for(var i : String in _tasksInitData){
    $modelsV.push(i);
}
TasksItemsIconsManager.instance.initData($modelsV);

当然:

/**
 * 1115.1
 * */
private function loadTaskIconKayer($iconPath : String = "" ) : void{
    if(!$iconPath){
        MJDebugger.info("---- ----任务图标路经错误",$iconPath);
    }
    TasksItemsIconsManager.instance.getIconByPath($iconPath,callBack);
}
/**
 * 1115.1
 * */
private function callBack($obj : * ,$path : String = "") : void{
    if($obj is Bitmap){
        var $bm : Bitmap = $obj as Bitmap;
        this.addChild($bm);
        $bm.x = 10;
        $bm.y = 7;
    }else{
        if($obj is Loader){
            var $myLoader : Loader = $obj as Loader;
            $myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,__onLoaded,false,0,true);
            $myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,__onLoadingError,false,0,true);
            $myLoader.load(new URLRequest($path),new LoaderContext(true));
        }
    }
}
private function __onLoaded( $e : Event ) : void{
    var $myLoaderInfo : LoaderInfo = $e.target as LoaderInfo;
    if ($myLoaderInfo != null && $myLoaderInfo.loader != null && $myLoaderInfo.loader.content && $myLoaderInfo.loader.content is Bitmap) {
        var $bm : Bitmap = $myLoaderInfo.loader.content as Bitmap;
        if($bm != null){
            var $bufferIcon : TasksItemsIconsDataModel = new TasksItemsIconsDataModel();
            $bufferIcon.iconPath = LoadingBiz.CLIENTURL+"res/task/"+taskSerial+".png";
            Debug.log("31115.1+URL+:=>"+$bufferIcon.iconPath);
            $bufferIcon.iconBitMap = $bm;
            TasksItemsIconsManager.instance.addNewModel($bufferIcon);
            Debug.log("31115.1---自我ICON");
            this.addChild($bm);
            $bm.x = 10;
            $bm.y = 7;
        }
        $myLoaderInfo.loader.unload();
    }
}
private function __onLoadingError( $e : IOErrorEvent ) : void{
    Debug.log("加载资源出错 ICON --- 31115.1");
}这样下次去图片时,直接取内存