美文网首页H5游戏开发Egret游戏开发
Egret微信小游戏好友排行榜教程

Egret微信小游戏好友排行榜教程

作者: 韩梅梅梅梅 | 来源:发表于2018-09-03 10:10 被阅读492次

    https://mp.weixin.qq.com/s/kYIdnHv-C5KuR9snekPNBg

    今天我们分享的菜鸟文档将介绍微信小游戏好友排行榜的制作过程,包括创建项目并发布、微信开发者平台添加小游戏、打开开放域功能、主域和开放域通讯,以及ShareCanvas与原生Canvas的布局。

    image

    图片来源于微信跳一跳排行榜

    微信好友排行榜利用微信关系链数据实现一个简单的排行榜,此文档包含关系链数据、Egret平台数据接口、ShareCanvas离屏画布、原生Canvas布局。

    创建项目并发布

    创建Egret项目,使用Launcher发布,建议使用您个人AppID(测试用的id限制很多功能,例如分享)

    image

    添加小游戏

    设置 > 基本设置 > 添加小程序(能够通过审核即可)

    image

    打开开放域功能

    使用微信开发者工具打开发布的微信小游戏,或在终端运行 egret run--target wxgame ,找到游戏配置文件 game.json,配置如下,其中 openDataContext使小游戏支持了微信开放域功能。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. {

    2. "deviceOrientation": "portrait",

    3. "networkTimeout": {

    4. "request": 5000,

    5. "connectSocket": 5000,

    6. "uploadFile": 5000,

    7. "downloadFile": 5000

    8. },

    9. "openDataContext": "openDataContext"

    10. }

    </pre>

    ShareCanvas介绍

    开放数据域的绘制文件中已经拥有一个通过Canvas API绘制的排行榜 ,SharedCanvas 是主域和开放数据域都可以访问的一个离屏画布,原理如下所示。

    image

    index.js文件中,官方已经为我们绘制了一个简单的排行榜demo,渲染出的效果如下图所示:

    image

    主域和开放域通讯

    开放域已经为我们绘制好了虚拟排行榜,现在只需要让主域打开开放域的排行榜就可以展示在Canvas上了。

    首先,创建开放数据域显示对象,离屏画布的显示对象可直接在主域中通过以下的方式进行创建,创建的显示对象为 egre.Bitmap 类型,可直接添加到舞台上。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. //在主域中创建开放数据域显示对象

    2. var platform = window.platform;

    3. this.bitmap = platform.openDataContext.createDisplayObject(null,this.stage.stageWidth, this.stage.stageHeight);

    </pre>

    其次,通过主域与开放数据域的单向数据接口进行通讯,主域可向开放数据域单方向发送消息。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. //主域向子域发送数据

    2. plathform.openDataContext.postMessage({

    3. isRanking: this.isRankClick,

    4. text: "egret",

    5. year: (new Date()).getFullYear(),

    6. command: "open"

    7. });

    </pre>

    子域可通过监听事件的方式获取到主域发送过来的自定义信息。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. //子域接收信息

    2. wx.onMessage((data) => {

    3. if (data.command == 'open') {

    4. //显示开放域

    5. if (!hasCreateScene) {

    6. //创建并初始化

    7. hasCreateScene = createScene();

    8. ...

    9. }

    10. }

    </pre>

    最后,开发者便可以在主域中发送数据,请求开放域打开排行榜,子域接收到数据打开排行榜。

    扩展

    您可以通过修改 index.js文件内的参数改变排行榜样式达到目标效果,可以使用自己的图片放到对应的路径中(总文件大小不要超过4M)。布局建议不要使用固定数字的数值,而是以 stageWidth stageHeight 舞台宽高作为基数,以尽量减少不同手机出现的适配问题。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. /**

    2. * 资源加载组,将所需资源地址以及引用名进行注册

    3. * 之后可通过assets引用名方式进行获取

    4. */

    5. var assets = {

    6. icon: "openDataContext/assets/icon.png",

    7. box: "openDataContext/assets/signIn.png",

    8. panel: "openDataContext/assets/bg.png",

    9. button: "openDataContext/assets/OK_button.png",

    10. title: "openDataContext/assets/rankingtitle.png"

    11. };

    </pre>

    image

    排行榜展示

    注意:安卓偶尔有不显示数据的BUG,在index.js中给字体加一个颜色即可。

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. //设置字体

    2. context.font = fontSize + "px Arial";

    3. context.fillStyle = "#fff"

    </pre>

    小结

    通过本文您可以对以下问题有更深入的了解

    • 对微信的关系链数据有更深入的理解

    • 平台数据接口的作用和使用

    • 熟悉主域与开放域数据通讯规则

    • 对原生Canvas的布局有所了解

    本文关键代码参考

    <pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px;">

    1. private isRankClick:boolean = false;

    2. private bitmap: egret.Bitmap;

    3. /**

    4. * 排行榜遮罩,为了避免点击开放数据域影响到主域,在主域中建立一个遮罩层级来屏蔽点击事件.</br>

    5. * 根据自己的需求来设置遮罩的 alpha 值 0~1.</br>

    6. */

    7. private rankingListMask: egret.Shape;

    8. //显示微信排行榜

    9. public obBtnRankingClick(e:egret.TouchEvent) {

    10. console.log("点击排行榜");

    11. let plathform:any = window.platform;

    12. if(!this.isRankClick) {

    13. //处理遮罩,避免开放域数据影响主域

    14. this.rankingListMask = new egret.Shape();

    15. this.rankingListMask.graphics.beginFill(0x000000,1);

    16. this.rankingListMask.graphics.drawRect(0,0,this.stage.width,this.stage.height);

    17. this.rankingListMask.graphics.endFill();

    18. this.rankingListMask.alpha = 0.4;

    19. //设置为true,以免触摸到下面的按钮

    20. this.rankingListMask.touchEnabled = true;

    21. this.addChildAt(this.rankingListMask,999);

    22. //让排行榜按钮显示在容器内

    23. this.addChild(this.btn_ranking);

    24. //显示开放域数据

    25. this.bitmap = plathform.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);

    26. this.addChild(this.bitmap);

    27. //主域向子域发送数据

    28. plathform.openDataContext.postMessage({

    29. isRanking: this.isRankClick,

    30. text: "egret",

    31. year: (new Date()).getFullYear(),

    32. command: "open"

    33. });

    34. this.isRankClick = true;

    35. }

    36. else {

    37. this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);

    38. this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);

    39. this.isRankClick = false;

    40. plathform.openDataContext.postMessage({

    41. isRanking: this.isRankClick,

    42. text: "egret",

    43. year: (new Date()).getFullYear(),

    44. command: "close"

    45. });

    46. }

    47. }

    </pre>

    本文源码链接: https://github.com/shenysun/FriendsList

    相关文章

      网友评论

        本文标题:Egret微信小游戏好友排行榜教程

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