分享几个egret常用类
- http请求类
// TypeScript file
class HttpRes extends egret.HttpRequest
{
private callback:Function = null;
public datas:any = null;
private httpUrl:string = "";
private httpData:any = "";
private httpType:string;
private httpStype:string;
constructor(call:Function) {
super();
this.callback = call;
}
public setUrl(urls:string,type:string,s_type:string,data:any) {
this.httpUrl = urls;
this.httpData = data;
this.httpType = type || "GET";
this.httpStype = s_type || "application/x-www-form-urlencoded";
}
public httpInit () {
this.responseType = egret.HttpResponseType.TEXT;
if(this.httpType == "GET") {
this.open(this.httpUrl,egret.HttpMethod.GET);
}else if(this.httpType == "POST") {
this.open(this.httpUrl,egret.HttpMethod.POST);
}else {
this.open(this.httpUrl,egret.HttpMethod.GET);
}
this.setRequestHeader("Content-Type",this.httpStype);
this.send(this.httpData);
this.addEventListener(egret.Event.COMPLETE,this.onGetComplete,this);
this.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError,this);
this.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress,this);
}
public onGetComplete () {
console.log("请求成功!");
this.datas = this.response;
if(this.callback != null) {
this.callback();
}
}
private onGetIOError () {
console.log("请求失败!");
}
private onGetProgress () {
console.log("请求之中!");
}
public getDatas ():any {
return this.datas;
}
}
- 工具类 ``` // TypeScript file
// 工具类
class GameUtil {
// 获取舞台高度
public static getStageHeight(): number {
return egret.MainContext.instance.stage.stageHeight
}
// 获取舞台宽度
public static getStageWidth(): number {
return egret.MainContext.instance.stage.stageWidth
}
// 获取宽度居中
public static getCenterW(w: number): number {
return (GameUtil.getStageWidth() - w) / 2
}
// 获取高度居中
public static getCenterH(h: number): number {
return (GameUtil.getStageHeight() - h) / 2
}
// 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
public static createBitmapByName(name: string, type: string = 'png') {
let result = new egret.Bitmap()
let texture: egret.Texture = RES.getRes(name + '_' + type)
result.texture = texture
return result
}
// 根据name关键字创建一个MovieClip对象。name属性请参考resources/resource.json配置文件的内容。
public static createMovieClipByName(name:string,MCname:string): egret.MovieClip {
let data_stay: any = RES.getRes(name + "_json")
console.log(data_stay)
let texture_stay: egret.Texture = RES.getRes(name + "_png")
let mcFactory_stay: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data_stay, texture_stay)
return new egret.MovieClip(mcFactory_stay.generateMovieClipData(MCname))
}
}
- 场景变换控制
/**实例对象 */
static sceneManager: SceneManager
/**获取实例 */
static get instance() {
if (!this.sceneManager) {
this.sceneManager = new SceneManager()
}
return this.sceneManager
}
/**
* 设置根场景
* @param main 根场景
*/
public setStage(main: egret.DisplayObjectContainer) {
this._stage = main
}
/**
* 切换场景
* @param scene 切换到的目标场景
* @param parentScene 需要切换到的父场景, 会移除该场景下所有的其他场景. 为空的时候, 默认为根场景
*/
static switchScene(scene: egret.DisplayObjectContainer, parentScene?: egret.DisplayObjectContainer) {
if (parentScene) {
parentScene.removeChildren()
parentScene.addChild(scene)
} else {
this.sceneManager._stage.removeChildren()
this.sceneManager._stage.addChild(scene)
}
}
/**
* 添加场景
* @param scene 添加的场景
* @param parentScene 需要添加到的场景. 为空的时候, 默认为根场景
*/
static addScene(scene: egret.DisplayObjectContainer, parentScene?: egret.DisplayObjectContainer) {
if (parentScene) {
parentScene.addChild(scene)
} else {
this.sceneManager._stage.addChild(scene)
}
}
/**
* 移除场景
* @param scene 移除的场景
* @param parentScene 父级场景. 为空的时候, 默认为根场景
*/
static removeScene(scene: egret.DisplayObjectContainer, parentScene?: egret.DisplayObjectContainer) {
if (parentScene) {
parentScene.removeChild(scene)
} else {
this.sceneManager._stage.removeChild(scene)
}
}
- 碰撞检测
/**碰撞检测 */ static hitTest(obj1: egret.DisplayObject, obj2: egret.DisplayObject): boolean { var rect1: egret.Rectangle = obj1.getBounds(); var rect2: egret.Rectangle = obj2.getBounds(); rect1.x = obj1.x; rect1.y = obj1.y; rect2.x = obj2.x; rect2.y = obj2.y; return rect1.intersects(rect2); }
- loadingUi 设计
> 在main.ts中更改loadingUi代码
private async loadResource() {
// try {
// const loadingView = new LoadingUI();
// this.stage.addChild(loadingView);
// await RES.loadConfig("resource/default.res.json", "resource/");
// await this.loadTheme();
// await RES.loadGroup("preload", 0, loadingView);
// this.stage.removeChild(loadingView);
// }
// catch (e) {
// console.error(e);
// }
———————————–上为源代码————————————————— try { await RES.loadConfig(“resource/default.res.json”, “resource/”);
// 加载loading Group
await RES.loadGroup("loading");
const loadingView = new LoadingUI();
this.stage.addChild(loadingView);
await this.loadTheme();
await RES.loadGroup("preload", 0, loadingView);
this.stage.removeChild(loadingView);
}
catch (e) {
console.error(e);
}
} ```
在loadUi中确保需要显示的资源已经加载好时即可使用