美文网首页
egret学习(2)

egret学习(2)

作者: 橙汁坤 | 来源:发表于2019-03-05 14:05 被阅读0次

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

相关文章

  • egret学习(2)

    分享几个egret常用类 http请求类 工具类 场景变换控制 碰撞检测 loadingUi 设计 在main.t...

  • KTX转换工具的使用

    一、egret官方提供的工具[http://docs.egret.com/engine/docs/2dRender...

  • learning egret

    learning egret Index 1.js如何调用ts?2.ts如何调用js?3.egret基础4.Q&A...

  • egret之HelloWorld

    标签: egret,入门 什么是Egret? Egret是一套HTML5游戏开发解决方案,产品包含Egret En...

  • egret微信小游戏项目图片管理

    egret微信小游戏项目图片管理 工具---Egret Launcher中下载 egret Texture Mer...

  • egret 白鹭引擎参考教学

    Egret Wing实战教程 Egret Wing实战教程(一):从按钮说起Egret Wing实战教程(二):自...

  • Egret eui学习

    eui 按钮、单选框、复选框 用法学习

  • Egret - 学习笔记

    1.纹理集实际上就是将一些零碎的小图放到一张大图当中。游戏中也经常使用到纹理集。使用纹理集的好处很多,我们通过将大...

  • egret学习(1)

    仅记录自学egret过程,希望能帮助他人 先记录几个常用的事件及基本概念 “显示对象”,是可以在舞台上显示的对象。...

  • egret 各个工具简浅作用说明

    egret launcher : 用来下载开发工具,以及引擎版本用主要工具:egret wing 3: egret...

网友评论

      本文标题:egret学习(2)

      本文链接:https://www.haomeiwen.com/subject/lhnnuqtx.html