egret笔记(2)

分享几个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中确保需要显示的资源已经加载好时即可使用

转载于:https://juejin.im/post/5c7e0f636fb9a04a006fc0f6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值