Egret之RES资源加载

对于资源加载这一块还是要记一下真的很重要!

//
// 1.加载资源配置文件    default.res.json
// 2.资源配置文件加载成功
// 3.资源配置文件加载失败
// 4.加载资源组
// 5.资源组加载成功
// 6.资源组加载失败
// 7.资源组中,单个资源加载失败
// 8.资源加载进入
//

 

class Main extends egret.DisplayObjectContainer{
    public constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
    };

    //Loading 视图
    private LoadingUI : LoadingUI;


    private onAddToStage (event:egret.Event){

        //加载资源配置文件
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE,this.onConfigComplate,this);
        RES.loadConfig("resource/default.res.json",'resource/')

        //实例化loading,并添加到舞台上
        this.LoadingUI = new LoadingUI();
        this.stage.addChild(this.LoadingUI)
    }




    //资源的配置文件加载完成
    private onConfigComplate(event:RES.ResourceEvent){

        //当资源配置文件加载完成,需要移除监听,放置内存泄漏
        RES.removeEventListener(RES.ResourceEvent .CONFIG_COMPLETE,this.onConfigComplate,this);

        //监听组加载时间
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);

        //加载组
        RES.loadGroup('game')
    }




    //组资源加载完成
    private onResourceLoadComplete(event: RES.ResourceEvent){
        //所有资源加载完成,需要移除资源加载的所有监听事件
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoadComplete,this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR,this.onResourceLoadError,this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS,this.onResourceProgress,this);
        RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR,this.onItemLoadError,this);
    }




    //资源加载进度
    private onResourceProgress(event: RES.ResourceEvent){
        console.log(event.itemsLoaded, event.itemsTotal)
        this.LoadingUI.setProgress(event.itemsLoaded,event.itemsTotal)

    }




    //组资源加载失败
    private onResourceLoadError(event: RES.ResourceEvent){
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        this.onResourceLoadComplete(event);
    }




    //单个组资源加载失败
    private onItemLoadError(event: RES.ResourceEvent): void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
        //忽略单个资源加载失败,继续加载其他资源
        this.onResourceLoadComplete(event);
    }





}

 egret项目优化:https://blog.csdn.net/Milan_KunderLa/article/details/83410158

资源加载:https://blog.51cto.com/aonaufly/1975063

                https://www.cnblogs.com/gamedaybyday/p/6079694.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值