Egret社区
本帖最后由 山猫 于 2018-8-31 11:25 编辑

本文将介绍开发微信小游戏四种常用功能的实现方法,这四种功能分别是:
- 获取头像功能
- 微信转发功能
- 微信分享功能- 游戏圈
在Egret Wing和微信开发者工具里的配置
为实现以上四个功能,我们分别需要在Egret Wing(图1,图2)和微信开发者工具(图3)里配置。
  
             图1
   
               图2

                图3
1. 需要在Platform.ts里调用platform.js接口。
2. 在Main.ts通过Platform.ts调用执行函数 。
3. 在 platform.js写相对应的逻辑代码。
以上三点是这四个微信小游戏的通用配置,具体操作如下:
获取头像
用户登录,可以获取用户自己的头像,参看[微信平台](https://developers.weixin.qq.com ... lity/authorize.html)。
Egret Wing,已经在Platform.ts写了默认功能,微信开发者工具已经写了默认逻辑,开发者只需要在Main添加代码
在Egret Wing—>src—>Main.ts添加以下代码
private async runGame() {
    const userInfo = await platform.getUserInfo();
        this.createGameScene(userInfo);   
}
protected createGameScene(userInfo:any): void {
// 用户头像
let img=new eui.Image();
    img.source=userInfo.avatarUrl
    this.addChild(img);
}微信小游戏转发功能
微信小游戏转发功能通过点击微信小游戏右上角按钮来触发小游戏的内置转发效果,达到转发给朋友的效果。
1. 在Egret Wing  —>  src  —>  Platform.ts添加以下代码
    declare interface  Platform {
         shop() : Promise<any>;
     }
    class DebugPlatform implements Platform {
        async shop() {}
    }
2. 在Egret Wing—>src—>Main.ts添加以下代码
     private async runGame() {
                platform.shop();
      }
3. 在微信开发者工具里Platform.ts添加以下代码
微信转发主要使用了wx.showShareMenu()和wx.onShareAppMessage()方法,具体参数可参看微信[开发平台](https://developers.weixin.qq.com ... -ability/share.html )
    class WxgamePlatform {
            shop() {
                return new Promise((resolve, reject) => {
                              wx.showShareMenu({
                                    withShareTicket: true
                              });
                          wx.onShareAppMessage(function () {
                            return {
                              title: "+++",
                              imageUrl: 'resource/assets/art/heros_goods/btnOK.png'
                            }
                            })

                    })
              }
        openDataContext = new WxgameOpenDataContext();
    }
微信小游戏分享功能
除了转发功能,我们也可以在微信小游戏内自定义一个按钮,主动分享给朋友。
1. 在Egret Wing —> src  —>  Platform.ts添加以下代码
   declare interface Platform {
          shareAppMessage() : Promise<any>;
   }
   class DebugPlatform implements Platform {
           async shareAppMessage(){}
   }
2. 在Egret wing—>src—>Main.ts添加以下代码
    protected createGameScene(): void {
    //游戏内自定义分享按钮
            let btnClose = new eui.Button();
                    btnClose.label = "分享";
                    btnClose.y = 300;
                    btnClose.horizontalCenter =180;
                    this.addChild(btnClose);
                    btnClose.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=>{
                        platform.shareAppMessage()
     }, this)
    }
3. 在微信开发者工具里Platform.ts添加以下代码
微信分享主要使用了shareAppMessage()方法,具体参数可参看微信[开发平台](https://developers.weixin.qq.com ... -ability/share.html)
     class WxgamePlatform {
            shareAppMessage() {
                 return new Promise((resolve, reject) => {
                    wx.shareAppMessage({
                    title: '转发标题',
                    imageUrl: 'resource/assets/art/heros_goods/btnOK.png'
                 })        
              })
            }
            openDataContext = new WxgameOpenDataContext();
     }
游戏圈
微信游戏圈,在这里和好友交流游戏心得。
1. 在Egret Wing  —>  src  —>  Platform.ts添加以下代
   declare interface Platform {
        createGameClubButton() : Promise<any>;   
   }
    class DebugPlatform implements Platform {
          async createGameClubButton(){}               
  }

2. 在Egret Wing—>src—>Main.ts添加以下代码
   private async runGame() {
       platform.createGameClubButton();
   }
3. 在微信开发者工具里platform.js添加以下代码
使用方法createGameClubButton().查看参看[微信平台 ](https://developers.weixin.qq.com ... lity/game-club.html)。
   clas s WxgamePlatform {
          wx.createGameClubButton({
                icon: 'green',
                style: {
                  left: 200,
                  top: 626,
                  width: 40,
                  height: 40
                }
              })
            openDataContext = new WxgameOpenDataContext();
     }
以上是微信小游戏四种常见功能的实现方法,希望对您有所帮助。
【新手教程1】4天完成一款小游戏 http://bbs.egret.com/thread-49996-1-1.html
【新手教程2】EUI卡牌游戏的制作 http://bbs.egret.com/forum.php?m ... =1&extra=#pid301469
【新手教程3】微信小游戏排行榜 http://bbs.egret.com/thread-50903-1-1.html










分享到 :
4 人收藏

7 个回复

倒序浏览
Jom  登堂入室 | 2018-9-4 12:05:41
怎么判断分享是否成功呢
乡村兽医  登堂入室 | 2018-9-19 15:27:44
Jom 发表于 2018-9-4 12:05
怎么判断分享是否成功呢

老哥,我也想问,你弄清楚没。。。请教下
bk13  登堂入室 | 2018-9-28 14:04:17
乡村兽医 发表于 2018-9-19 15:27
老哥,我也想问,你弄清楚没。。。请教下

现在没法判断是否分享成功了,微信把这个返回值取消了
csy  登堂入室 | 2018-10-8 20:33:05
10.10 以后。。分享还可以用什么方法判断成功与否。。还是没啥方法判断了
h312903294  登堂入室 | 2018-10-29 09:48:07
图片看不到呢
PulesTree  初学乍练 | 2019-3-14 18:25:38
头像是怎么取的呀?有没有demo呀?
victoirechan  登堂入室 | 2021-9-15 11:34:41
学习中
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|京网文[2014]0791-191号|京ICP证150115号|Egret社区 ( 京ICP备14025619号 )

Powered by Discuz! X3.4 © 2001-2019 Comsenz Inc.

返回顶部