Egret白鹭开发小游戏之自定义load加载界面

Stella981
• 阅读 877

刚接触不久就遇到困难------自定义loading。想和其他获取图片方式一样获取加载界面的图片,结果发现资源还没加载就需要图片,在网上百度了许多,都没有找到正确的方式,通过自己的摸索,终于,,,我成功了。。。

下面介绍一下主要思想:

首先,我们需要使用异步加载的方式,在加载界面之前加载loading界面需要的素材,然后再loadingUI中就可以大胆使用它了。

其次,我们经常碰到的问题是自定义进度条不显示问题。那是因为你没有在Main中把它加载舞台上。

最后,看看具体实现方法吧。

1.新建load界面需要的资源组loading

Egret白鹭开发小游戏之自定义load加载界面

2.添加load界面需要的图片,并加入配置表相应位置

Egret白鹭开发小游戏之自定义load加载界面

3.main中添加代码:

Egret白鹭开发小游戏之自定义load加载界面

private loadingView: LoadingUI;

Egret白鹭开发小游戏之自定义load加载界面

private async runGame() {
        await this.loadResource();
        await this.loadConfig();
        this.stage.removeChild(this.loadingView);
        this.initGame();
        // const result = await RES.getResAsync("description_json")
        // this.startAnimation(result);
        await platform.login();
        const userInfo = await platform.getUserInfo();
        console.log(userInfo);

    }
    private async loadResource() {
        try {
            await RES.loadConfig("resource/default.res.json", "resource/");
            await RES.loadGroup("loading")
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            await this.loadTheme();
            await RES.loadGroup("preload", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }
    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                //设置加载进度界面
                this.loadingView = new LoadingUI();
                this.stage.addChild(this.loadingView);
                resolve();
            }, this);

        })
    }
    private onResourceProgress(event: RES.ResourceEvent): void {
        if (event.groupName == "preload") {
            this.loadingView.onProgress(event.itemsLoaded, event.itemsTotal);
        }
    }

4.在LoadingUI中修改代码为:

class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {

    public constructor() {
        super();
        if (!this.pBar) {
            this.createView();
        }
    }
    private pBar: eui.ProgressBar;
    private bg:egret.Bitmap;


    private async createView(){

        this.bg=new egret.Bitmap();
        this.bg.texture=RES.getRes("开始界面_png");
        // this.bg.width=this.stage.stageWidth;
        // this.bg.height=this.stage.stageHeight;
        this.addChild(this.bg);

        this.pBar = new eui.ProgressBar();
        this.pBar.x = 400;
        this.pBar.y = 600;
        this.pBar.width = 1000;
        this.pBar.height = 40;
        this.pBar.maximum = 100;
        this.pBar.minimum = 0;
        this.pBar.value = 0;
        this.addChild(this.pBar);
    }
    private createBitmapByName(name: string): egret.Bitmap {
        var result: egret.Bitmap = new egret.Bitmap();
        var texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }
    public onProgress(current: number, total: number): void {
        if (this.pBar.labelDisplay != null || this.pBar.labelDisplay != undefined) {
            // egret.log("加载进度条~~~~~");
            this.pBar.labelDisplay.textColor = 0xff0000;
            this.pBar.value = current;
        }

    }
}

至此,自定义加载界面完成,当然,你还可以根据自己喜爱添加,修改加载界面布局

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
Vue.js的图片加载性能优化你可以试试
前言图片加载优化对于一个网站性能好坏起着至关重要的作用。所以我们使用Vue来操作一波。备注以下的优化一、优化二栏目都是我自己封装在Vue的工具函数里,所以请认真看完,要不然直接复制的话,容易出错的。资源Vue.jsElementUI优化一:图片加载动画只有当图片加载完成后才可以显示图片,加载动画结束。我们使用ElementUI
Karen110 Karen110
2年前
一篇文章教会你使用JS+CSS实现一个简单加载进度条的效果
大家好,我是前端进阶者,今天给大家来做个小项目,一起来看看吧一、前言我们经常在网页上,游戏界面加载时会看到加载进度条的效果,我们往往会以为这些加载进度条的效果,很难实现。今天教大家JSCSS结合做简单一个加载进度条的效果。二、项目准备软件:HBuilderX。三、项目实现1\.body创建2个div,外部div添加id"progress"属
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
浩浩 浩浩
3年前
【Flutter实战】图片和Icon
3.5图片及ICON3.5.1图片Flutter中,我们可以通过Image组件来加载并显示图片,Image的数据源可以是asset、文件、内存以及网络。ImageProviderImageProvider是一个抽象类,主要定义了图片数据获取的接口load(),从不同的数据源获取图片需要实现不同的ImageProvi
Stella981 Stella981
2年前
Android 经典笔记七 全局弹窗Dialog
目录介绍1.全局弹窗分析2.全局弹窗必要条件3.全局弹窗实现方式3.1.利用系统弹出dialog3.2.获取WindowManager,直接添加view3.3.在服务里,获取栈顶的Activity,弹窗4.Dialog实现全局Loading加载框4.1.自定义Loading类4.2.给自定义的
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
Cocos Creator 资源加载流程剖析【三】——Load部分
Load流程是整个资源加载管线的最后一棒,由Loader这个pipe负责(loader.js)。通过Download流程拿到内容之后,需要对内容做一些“加载”处理。使得这些内容可以在游戏中使用。这里并不是所有的资源都需要进行一个加载处理,目前只有图片、Json、Plist、Uuid(Prefab、场景)等资源才会执行加载的流程,其他的资源在Downloa
Stella981 Stella981
2年前
DirectX3D设备丢失(lost device)的处理(一)
在创建时使用D3DPOOL\_MANAGED标志的资源可以不需要重新载入,但D3DPOOL\_DEFAULT加载的资源就需要先释放,后重建。通常需要这样处理的有ID3DXFont和ID3DXSprite,而.X模型什么的就不需要。在发现设备丢失时,我们要调用 OnLostDevice(void)函数让D3DPOOL\_DEFAULT加载的资
Stella981 Stella981
2年前
GridManager loading样式修改
在使用gridmanager表格组件时,如果想要改变loading样式该如何处理?gridmanager提供的loadingTemplate参数可以快速的解决这个问题。loadingTemplate参数的介绍:参数类型:String默认值:''数据加载中模板,该配置可以自定义数据加载时使用的loading样式。使用该