一 , 獲取資源的3種方式:
①:RES.getRes(name:string):any
同步獲取資源 這種方式只能獲取已經緩存過的資源
②:RES.getResAsync(name:string,compFunc:Function,thisObject:any):void
異步獲取資源,這種方式可以獲取配置中含有的所有資源項。如果緩存中存在,直接調用回調函數返回,若不存在,就啟動網絡加載文件并解析后回調。
③:RES.getResByUrl(url:string,compFunc:Function,thisObject:any,type:string=””):void
通過url獲取不在配置中的資源,通常不建議使用這個接口,只有那些不合適填寫在配置中,比如獲取網絡上其他服務器的資源時,才采用這種方式。
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
}
private onAddToStage(event:egret.Event) {
RES.getResByUrl('/resource/assets/bg.jpg',this.onComplete,this,RES.ResourceItem.TYPE_IMAGE);
}
private onComplete(event:any):void {
var img: egret.Texture = <egret.Texture>event;
var bitmap: egret.Bitmap = new egret.Bitmap(img);
this.addChild(bitmap);
}二 , 加載資源組的步驟
1 : 加載資源配置json文件,如resouce.res.json.
RES.addEventListener( RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this );
RES.addEventListener( RES.ResourceEvent.CONFIG_LOAD_ERROR, this.onConfigLoadErr, this );
RES.loadConfig("resources/resource.res.json","resources/");解析 :
①:RES.loadConfig
參數1:json配置文件的路徑 。 參數2 : 子資源的位置
2: 加載json配置中的某一個資源組 , 比如加載preload資源組
private onConfigComplete(event: RES.ResourceEvent): void {
RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, 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("preload");
}3:偵聽加載事件:
/**
* preload資源組加載完成
*/
private onResourceLoadComplete(event: RES.ResourceEvent) {
if (event.groupName == "preload") {
this.stage.removeChild(this.loadingView);
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 onItemLoadError(event: RES.ResourceEvent) {
console.warn("Url:" + event.resItem.url + " has failed to load");
}
/**
* 資源組加載出錯
*/
private onResourceLoadError(event: RES.ResourceEvent) {
console.warn("Group:" + event.groupName + " has failed to load");
//忽略加載失敗的項目
this.onResourceLoadComplete(event);
}
/**
* preload資源組加載進度
*/
private onResourceProgress(event: RES.ResourceEvent) {
if (event.groupName == "preload") {
this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
}
}三 , 自定義加載組加載資源(重點)
在json中建立資源,這次資源不放在任何組中,本人使用代碼自動構建組進行加載

加載管理器:
module app{
/**
* 構建組RES并加載
* @author Aonaufly
*/
export class AutoGroupResLoardManager{
private _loaderArr : Array<IAutoGroupResLoarData> = null;
private _is_loading : boolean = false;//是否正在加載中
private static _instance : AutoGroupResLoardManager = null;
/**當前正在加載的資源數據*/
private _cur_data : IAutoGroupResLoarData = null;
public static get Ins() : AutoGroupResLoardManager{
if( AutoGroupResLoardManager._instance == null ) AutoGroupResLoardManager._instance = new AutoGroupResLoardManager();
return AutoGroupResLoardManager._instance;
}
public constructor( ){
this._loaderArr = [];//數據加載的集合
}
/**
* 開始加載接口
* @param {app.IAutoGroupResLoarData} data
*/
public startLoader( data : IAutoGroupResLoarData ) : void{
if( this._is_loading ){
this._loaderArr.push( data );//放入加載列表等待加載
}else{
this.createGroup2Res( data );
}
}
/**
* 構建組并加載
* @param {app.IAutoGroupResLoarData} data
*/
private createGroup2Res( data : IAutoGroupResLoarData ) : void{
if( RES.createGroup( data._groupName , data._arr2Res , true )){
this._cur_data = data;
this.onConfigComplete(data);
}else{
//構建失敗
data._callBack( TypeCallBack2AutoGroupResLoar.CREATE_GROUP_ERROR );
this.loaderNext();//繼續加載下一個
}
}
/**
* 加載下一個
*/
private loaderNext() : void{
if( this._loaderArr.length > 0 ){
let data : IAutoGroupResLoarData = this._loaderArr.shift();
this.createGroup2Res( data );
}else{
RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoad, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR,this.onResourceLoad, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS,this.onResourceLoad, this);
RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR,this.onResourceLoad, this);
}
}
/**
* 添加加載偵聽
* @param {app.IAutoGroupResLoarData} data
*/
private onConfigComplete(data : IAutoGroupResLoarData): void {
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoad, this);
RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoad, this);
RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceLoad, this);
RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onResourceLoad, this);
RES.loadGroup(data._groupName);
}
/**
* 加載偵聽處理
* @param {RES.ResourceEvent} $e
*/
private onResourceLoad( $e : RES.ResourceEvent ):void{
switch( $e.type ){
case RES.ResourceEvent.GROUP_COMPLETE:
if( $e.groupName == this._cur_data._groupName ){
this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE , this._cur_data._class );//加載完成
this.loaderNext();//加載下一個
}
break;
case RES.ResourceEvent.GROUP_LOAD_ERROR:
if( $e.groupName == this._cur_data._groupName ){
this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE , this._cur_data._class);//容錯加載完成
this.loaderNext();
}
break;
case RES.ResourceEvent.GROUP_PROGRESS://加載進度
if($e.groupName == this._cur_data._groupName){
this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_PROGRESS , this._cur_data._class ,$e );
}
break;
case RES.ResourceEvent.ITEM_LOAD_ERROR:
if($e.groupName == this._cur_data._groupName){
this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.ITEM_LOAD_ERROR , this._cur_data._class ,$e);
console.log("Url:" + $e.resItem.url + " has failed to load");
}
break;
}
}
}
/**
* 自動加載數據
* @author Aonaufly
*/
export interface IAutoGroupResLoarData{
/**
* 組名
*/
_groupName : string;
/**
* 資源名
*/
_arr2Res : Array<string>;
/**
* 回調方法
*/
_callBack : Function;
_class : any;
}
/**
* 毀掉類型枚舉
*/
export enum TypeCallBack2AutoGroupResLoar{
/**構建失敗*/
CREATE_GROUP_ERROR = 0,
/**組加載完成*/
GROUP_COMPLETE = 1,
/**組加載進度*/
GROUP_PROGRESS = 2,
/**組中元素加載失敗*/
ITEM_LOAD_ERROR = 3
}
}測試調用:
module app {
export class FntView extends eui.Component implements eui.UIComponent{
private group_number : eui.Group;
private img_webp : eui.Image;
public constructor() {
super();
this.skinName = "resource/eui_skins/FntView.exml";
}
protected childrenCreated():void{
super.childrenCreated();
this.showNumer( 1139 );
//this.img_webp.source = RES.getRes("bg_jpg");
var texture:egret.Texture = RES.getRes("aab_webp");
var result: egret.Bitmap = new egret.Bitmap();
result.texture = texture;
result.width = 500;
result.height = 500;
this.addChild(result);
this.loaderRES();
}
private showNumer( num : number ) : void{
let show : string = num.toString();
FntManager.showFnt( this.group_number , "" , show , 125 , 75 );
}
//////////////////////// RES加載器的使用
private loaderRES():void{
let resData : IAutoGroupResLoarData = {
_groupName : "test2create",
_arr2Res : [
"c_01_jpg",
"c_02_jpg"
],
_callBack : this.callBack2Res,
_class : this
};
AutoGroupResLoardManager.Ins.startLoader( resData );
}
private callBack2Res( type : TypeCallBack2AutoGroupResLoar , $class : FntView = null , $e : RES.ResourceEvent = null ) : void{
switch ( type ){
case TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE:
//加載完成
let img01: eui.Image = new eui.Image();
img01.source = RES.getRes("c_01_jpg");
let img02: eui.Image = new eui.Image();
img02.source = RES.getRes("c_02_jpg");
img01.x = 100;
img01.y = 300;
img02.x = 300;
img02.y = 300;
$class.addChild(img01);
$class.addChild(img02);
break;
case TypeCallBack2AutoGroupResLoar.CREATE_GROUP_ERROR:
console.error("創建組 : test2create 失敗");
break;
case TypeCallBack2AutoGroupResLoar.GROUP_PROGRESS:
console.log(`進度 : ${$e.itemsLoaded}/${$e.itemsTotal}`);//打印進度
break;
case TypeCallBack2AutoGroupResLoar.ITEM_LOAD_ERROR:
console.log(`自定義加載組 : ${$e.groupName} , 錯誤單元 : ${$e.resItem.url}`);
break;
}
}
}
}結果:

控制臺打印結果:

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。