推荐阅读:

最近事情特别多,今天终于实现了排行榜功能,记录下来大家一起学习学习。

一。调用默认排行榜

首先我们需要了解:

1。白鹭开发文档(开放数据域):http://developer.egret.com/cn/github/egret-docs/Engine2D/minigame/openDataContext/index.html

2.微信小游戏开发文档(关系链数据):https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/open-data.html

实现的核心功能是在main.ts中添加代码

(1)添加全局变量

(2)定义排行榜按钮,加载排行榜资源

(3)给按钮添加监听事件

main.ts中全部代码如下:

class Main extends eui.UILayer {

    private bitmap: egret.Bitmap;
private isdisplay = false;
private rankBtn: eui.Button;
private rankingListMask: egret.Shape; protected createChildren(): void {
super.createChildren(); egret.lifecycle.addLifecycleListener((context) => {
// custom lifecycle plugin
context.onUpdate = () => { }
}) egret.lifecycle.onPause = () => {
egret.ticker.pause();
} egret.lifecycle.onResume = () => {
egret.ticker.resume();
} //inject the custom material parser
//注入自定义的素材解析器
let assetAdapter = new AssetAdapter();
egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter()); this.runGame().catch(e => {
console.log(e);
})
} private async runGame() {
await this.loadResource()
this.createGameScene();
// const result = await RES.getResAsync("description_json")
// this.startAnimation(result);
await platform.login();
await platform.showShareMenu();
const userInfo = await platform.getUserInfo();
console.log(userInfo);
// await platform.showShareMenu();
} 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", , 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, () => {
resolve();
}, this); })
}
/**
* 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
* Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
*/
private createBitmapByName(name: string): egret.Bitmap {
let result = new egret.Bitmap();
let texture: egret.Texture = RES.getRes(name);
result.texture = texture;
return result;
}
/**
* 描述文件加载成功,开始播放动画
* Description file loading is successful, start to play the animation
*/
private startAnimation(result: Array<any>): void {
// let parser = new egret.HtmlTextParser(); // let textflowArr = result.map(text => parser.parse(text));
// let textfield = this.textfield;
// let count = -1;
// let change = () => {
// count++;
// if (count >= textflowArr.length) {
// count = 0;
// }
// let textFlow = textflowArr[count]; // // 切换描述内容
// // Switch to described content
// textfield.textFlow = textFlow;
// let tw = egret.Tween.get(textfield);
// tw.to({ "alpha": 1 }, 200);
// tw.wait(2000);
// tw.to({ "alpha": 0 }, 200);
// tw.call(change, this);
// }; // change();
}
private textfield: egret.TextField;
/**
* 创建场景界面
* Create scene interface
*/
protected createGameScene(): void {
let sky = this.createBitmapByName("bg_jpg");
this.addChild(sky);
let stageW = this.stage.stageWidth;
let stageH = this.stage.stageHeight;
sky.width = stageW;
sky.height = stageH;
//打开关闭排行榜按钮
this.rankBtn = new eui.Button();
this.rankBtn.label = "排行";
this.rankBtn.x = ;
this.rankBtn.y = ;
this.addChild(this.rankBtn);
this.rankBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickRankBtn, this);
//加载资源
const platform: any = window.platform;
platform.openDataContext.postMessage({
command: 'loadRes'
}); } private onClickRankBtn() {
console.log('点击btnClose按钮');
let platform: any = window.platform;
if (this.isdisplay) {
this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);
this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);
this.isdisplay = false;
platform.openDataContext.postMessage({
isDisplay: this.isdisplay,
text: 'hello',
year: (new Date()).getFullYear(),
command: "close"
});
} else {
//处理遮罩,避免开放数据域事件影响主域。
this.rankingListMask = new egret.Shape();
this.rankingListMask.graphics.beginFill(0x000000, );
this.rankingListMask.graphics.drawRect(, , this.stage.width, this.stage.height);
this.rankingListMask.graphics.endFill();
this.rankingListMask.alpha = 0.5;
this.rankingListMask.touchEnabled = true;
this.addChild(this.rankingListMask); //简单实现,打开这关闭使用一个按钮。
this.addChild(this.rankBtn);
//主要示例代码开始
this.bitmap = platform.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);
this.addChild(this.bitmap);
//主域向子域发送自定义消息
platform.openDataContext.postMessage({
isDisplay: this.isdisplay,
text: 'hello',
year: (new Date()).getFullYear(),
command: "open"
});
//主要示例代码结束
this.isdisplay = true;
}
} }
在微信开发工具上预览效果:

此时如果你在手机上预览,会出现如下效果

你会发现key,name,scroes显示的字体不见了。(可能是到了手机上,字体就默认为白色的原因吧)

这时候,我们需要在index.js里面改变字体颜色:

  context.fillStyle="#FF0000";

上面步骤我们只实现了基本的调用开放数据域中的排行,排行榜的具体样式,可以根据实际情况在开发者工具上的index.js中作修改调整。

最后,附上官方排行榜demo,供大家参考学习:

https://github.com/egret-labs/egret-target-wxgame

二。实现自定义排行榜

当然,上面的代码只是简单的调用了默认的排行榜,那么,如何做出自定义排行榜呢,为了研究这个问题,又消耗了我一周的时间研究,期间看了许多排行榜的代码,终于,在错误中蹦跶出来了,开心,下面跟大家分享一下我遇到的最大问题:

这个错误,我问了若干人,给我的回答是资源路径问题或者不存在导致,可是博主检查了几十遍发现没问题,终于在今天,我发现自己是多么的愚蠢,原来是未发送load命令,导致资源未加载,从而出现的错误,需要demo实例的朋友可以点击右侧加群下载。

原文链接:https://www.mmzsblog.cn/articles/2019/09/10/1568118032212.html

Egret白鹭开发微信小游戏排行榜功能的更多相关文章

  1. Egret白鹭开发微信小游戏分享功能

    今天给大家分享一下微信分享转发功能,话不多说,直接干 方法一: 1.在egret中打开Platfrom.ts文件,添加代码如下(当然,你也可以直接复制粘贴) /** * 平台数据接口. * 由于每款游 ...

  2. Egret白鹭开发微信小游戏程序跳转功能(由一个小游戏跳转到另一个小游戏)

    假设我们要实现的功能是从小游戏A跳转到小游戏B 对于小游戏A: (1)在platform.ts中添加代码如下: /** * 平台数据接口. * 由于每款游戏通常需要发布到多个平台上,所以提取出一个统一 ...

  3. Egret白鹭开发微信小游戏(使用皮肤搭建UI,代码调用组件功能)

    (1)新建皮肤,并命名如下 (2)根据实际情况自定义皮肤,例如: (3)修改名字为如下: (4)运行游戏会自动生成以下代码: (5)在default.thm.json中添加如下代码:(具体路径名字根据 ...

  4. Egret白鹭开发微信小游戏手机震动功能

    最近一直在修改调整项目,没有接触新功能,今天终于有机会,去翻了微信API,发现手机震动的API,今天分享出来大家一起学习学习 对于震动,微信提供了两个API,分别是: wx.vibrateShort: ...

  5. 【转】利用 three.js 开发微信小游戏的尝试

    前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...

  6. 使用Laya引擎开发微信小游戏(上)

    本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...

  7. 使用Laya引擎开发微信小游戏

    在支持微信小游戏的游戏引擎中,Cocos,Egret,Laya都对小游戏的开发提供了很多强大的支持.前段时间正好抽空研究了一下这块的内容,现做一个总结,针对如何使用Laya引擎开发微信小游戏给大家做一 ...

  8. cocosCreator微信小游戏排行榜思路

    cocosCreator制作微信小游戏排行榜实现方案: 游戏认知:项目分为主域和子域,主域就是游戏主程部分,子域为单独处理微信排行榜公共域数据的. 游戏主域里创建一个节点,添加WXSubContext ...

  9. 微信小游戏排行榜页滚动查看排行榜(canvas指定区域溢出滚动,懒渲染)

    在微信小游戏中,好友排名数据是能在关系数据域操作,整个关系数据域只会返回一个最终的sharedCanvas,并且这个canvas不能调用toDataURL()方法,所以要展示好友排行榜的话只能在关系数 ...

随机推荐

  1. linux 使用git

    linux下使用git管理代码   环境:centos7.2 :    工具:xshell 1.安装git,命令如下: #yum install git 2.设置用户名和邮箱,命令如下: #git c ...

  2. Redis 学习笔记(篇六):数据库

    Redis 是一个使用 C 语言编写的 NoSql 的数据库,本篇就讲解在 Redis 中数据库是如何存储的?以及和数据库有关的一些操作. Redis 中的所有数据库都保存在 redis.h/redi ...

  3. 如何启用linux的路由转发功能

    如何使用iptables的NAT功能把红帽企业版Linux作为一台路由器使用? 方法: 提示: 以下方法只适用于红帽企业版Linux 3 以上. 1.打开包转发功能: echo "1&quo ...

  4. Jenkins持续部署-创建差量更新包

    目录 Jenkins持续部署-创建差量更新包 目录 前言 目的 详细流程 生成版本号 获取版本号 创建文件更新清单 压缩 获取上个版本的包 创建差量更新包 读取服务器Json配置 远程创建文件夹目录 ...

  5. Java类什么情况下被初始化?

    1.创建类的实例(new 的方式).访问某个类或接口的静态变量,或者对该静态变量赋值,调用类的静态方法 2.反射的方式 3.当初始化一个类的时候,如果发现其父类还没有进行初始化,则需先触发其父类的初始 ...

  6. django第四次(转自刘江)

    我们都知道对于ManyToMany字段,Django采用的是第三张中间表的方式.通过这第三张表,来关联ManyToMany的双方.下面我们根据一个具体的例子,详细解说中间表的使用. 一.默认中间表 首 ...

  7. Mysql优化(出自官方文档) - 第五篇

    目录 Mysql优化(出自官方文档) - 第五篇 1 GROUP BY Optimization 2 DISTINCT Optimization 3 LIMIT Query Optimization ...

  8. chapter01作业

    1. 请用命令查出ifconfig命令程序的绝对路径 [root@localhost chen]# which ifconfig /usr/sbin/ifconfig 2.请用命令展示以下命令哪些是内 ...

  9. PyQt4 在Windows下安装

    快来加入群[python爬虫交流群](群号570070796),发现精彩内容.     首先在网上下载sip文件下载完之后解压, 在Windows的开始菜单栏中进入sip的解压目录下:   在目录下面 ...

  10. 初次见面C#排坑记录

    排一次开发C#遇到的坑,同时说一下自己对C#中文件夹构建方式的体会. 一个开发Java的人留下了痛苦的泪水,变量命名规则不一样,连括号打的都不一样,