本篇文章给大家带来的内容是关于微信小游戏中如何实现转发&分享&获取头像&游戏圈四种功能,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
今天我们分享的菜鸟教程文档将介绍开发微信小游戏四种常用功能的实现方法,期望能和开发者朋友们交流,非常欢迎大家给我们留言反馈。
这四种功能分别是:
获取头像功能
微信转发功能
微信分享功能
游戏圈
在egret wing和微信开发者工具里的配置
为实现以上四个功能,我们需要分别在egret wing(图1,图2)和微信开发者工具(图3)里配置。
需要在platform.ts里调用platform.js接口。
在main.ts通过platform.ts调用执行函数 。
在 platform.js写相对应的逻辑代码。
以上三点是实现四个微信小游戏功能的通用配置,具体操作如下:
获取头像
用户登录,可以获取用户自己的头像,参看微信平台。
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()方法,具体参数可参看微信开发平台
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(){}}
在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()方法,具体参数可参看微信开发平台
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().查看参看微信平台
class wxgameplatform { wx.creategameclubbutton({ icon: 'green', style: { left: 200, top: 626, width: 40, height: 40 } }) opendatacontext = new wxgameopendatacontext();}
以上是微信小游戏四种常见功能的实现方法,希望对您有所帮助。
相关推荐:
微信公众平台开发实现2048游戏的方法
五个微信小游戏的技术要点
以上就是微信小游戏中如何实现转发&分享&获取头像&游戏圈四种功能的详细内容。