default.res.json文件内容
{
"groups": [
{
"keys": "bg_jpg,description_json",
"name": "preload"
},
{
"keys": "avatar_default_png,back_png,login_wx_png",
"name": "stload"
},
{
"keys": "bg_jpg",
"name": "defaultload"
}
],
"resources": [
{
"url": "assets/bg.jpg",
"type": "image",
"name": "bg_jpg"
},
{
"url": "config/description.json",
"type": "json",
"name": "description_json"
},
{
"url": "assets/lobby/avatar_default.png",
"type": "image",
"name": "avatar_default_png"
},
{
"url": "assets/lobby/back.png",
"type": "image",
"name": "back_png"
},
{
"url": "assets/lobby/login_wx.png",
"type": "image",
"name": "login_wx_png"
}
]
}
Main.ts代码内容
//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014-present, Egret Technology.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the Egret nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
class Main extends egret.DisplayObjectContainer {
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addStage, this);
}
public addStage(event: egret.Event): void {
RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);
RES.loadConfig("resource/default.res.json", "resource/");
let e = event;
}
//根据名为preload的group来加载,最不灵活
public configComplete(event: RES.ResourceEvent): void {
RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.groupComplete, this);
RES.loadGroup("defaultload");//defaultload是在default.res.json中配置的资源组
let e = event;
}
public groupComplete(event: RES.ResourceEvent): void {
RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.groupComplete, this);
let bg = new egret.Bitmap();
bg.texture = RES.getRes("bg_jpg");
bg.x = 0;
bg.y = 0;
this.addChild(bg);
let fill = new egret.Shape();
fill.graphics.beginFill(0xfffffe, 1);
fill.graphics.drawCircle(0, 0, this.getHeight() / 2);
fill.graphics.endFill();
fill.x = this.getWidth() / 2;
fill.y = this.getHeight() / 2;
this.addChild(fill);
let e = event;
this.dynamicLoadConfig();
}
//使用绝对路径来加载,不需要加载default.res.json了,最灵活
public lobby = "resource/assets/lobby/";
public resNames: string[] = [];
public resMap: {} = {};
public dynamicLoadConfig() {
this.resNames.push(this.lobby.concat("back.png"));
for (let k in this.resNames) {
egret.log("k = ", k, ", v = ", this.resNames[k]);
RES.getResByUrl(this.resNames[k], this.dynamicProcess, this);
}
}
public dynamicProcess(res: any, url: string): void {
if (!res) {
egret.error("error url = ", url, ", res === null");
} else {
egret.log("ok url = ", url, ", res not null");
}
this.resMap[url] = res;
if (Object.keys(this.resMap).length === Object.keys(this.resNames).length) {
this.dynamicLoadComplete();
}
}
public dynamicLoadComplete() {
let back = new egret.Bitmap();
back.texture = RES.getRes(this.lobby.concat("back.png"));
back.x = this.getWidth() / 2;
back.y = this.getHeight() / 2;
this.addChild(back);
this.createGroup();
}
//从已经添加到default.res.json中的资源中选取任意几张图片进行加载
//还是要配合RES.loadConfig("resource/default.res.json", "resource/");使用,但灵活一些了
public stImages: string[] = [];
public stMap: { [key: string]: any } = {};
public createGroup() {
this.stImages.push("avatar_default_png");
this.stImages.push("login_wx_png");
let group = "st_group";//要确保
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.stLoadComplete, this);
RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.stLoadProcess, this);
RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.stLoadError, this);
RES.createGroup(group, this.stImages);
RES.loadGroup(group);
}
public stLoadProcess(event: RES.ResourceEvent): void {
egret.log("stLoadProcess ", event.resItem.name);
this.stMap[event.resItem.name] = this.stMap[event.resItem.name];
}
public stLoadError(event: RES.ResourceEvent): void {
egret.error("stLoadError ");
}
public stLoadComplete(event: any): void {
RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.stLoadComplete, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.stLoadProcess, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.stLoadError, this);
egret.log("stLoadComplete")
let e = 0;
egret.log("stMap = ", Object.keys(this.stMap));
this.stImages.push("avatar_default_png");
this.stImages.push("login_wx_png");
let weixin = new egret.Bitmap();
weixin.texture = RES.getRes("login_wx_png");
weixin.x = this.getWidth() / 4;
weixin.y = this.getHeight() / 2;
this.addChild(weixin);
let avatar = new egret.Bitmap();
avatar.texture = RES.getRes("avatar_default_png");
avatar.x = this.getWidth() * (2 / 3);
avatar.y = this.getHeight() / 2;
this.addChild(avatar);
}
public getWidth(): number {
return egret.MainContext.instance.stage.stageWidth;
}
public getHeight(): number {
return egret.MainContext.instance.stage.stageHeight;
}
}