一 , 获取资源的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(ev