您好,登錄后才能下訂單哦!
本文將介紹開發(fā)微信小游戲四種常用功能的實現(xiàn)方法,這四種功能分別是:
**獲取頭像功能
微信轉(zhuǎn)發(fā)功能
微信分享功能
游戲圈**
在Egret Wing和微信開發(fā)者工具里的配置
為實現(xiàn)以上四個功能,我們需要分別在Egret Wing(圖1,圖2)和微信開發(fā)者工具(圖3)里配置。
以上三點是實現(xiàn)四個微信小游戲功能的通用配置,具體操作如下:
獲取頭像
用戶登錄,可以獲取用戶自己的頭像,參看微信平臺。
Egret Wing,已經(jīng)在Platform.ts寫了默認功能,微信開發(fā)者工具已經(jīng)寫了默認邏輯,開發(fā)者只需要在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);
}
微信小游戲轉(zhuǎn)發(fā)功能
微信小游戲轉(zhuǎn)發(fā)功能通過點擊微信小游戲右上角按鈕來觸發(fā)小游戲的內(nèi)置轉(zhuǎn)發(fā)效果,達到轉(zhuǎn)發(fā)給朋友的效果。
declare interface Platform {
shop():Promise<any>;
}
class DebugPlatform implements Platform {
async shop() {}
}
private async runGame() {
platform.shop();
}
微信轉(zhuǎn)發(fā)主要使用了wx.showShareMenu()和wx.onShareAppMessage()方法,具體參數(shù)可參看微信開發(fā)平臺
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();
}
微信小游戲分享功能
除了轉(zhuǎn)發(fā)功能,我們也可以在微信小游戲內(nèi)自定義一個按鈕,主動分享給朋友。
declare interface Platform {
shareAppMessage():Promise<any>;
}
class DebugPlatform implements Platform {
async shareAppMessage(){}
}
protected createGameScene(): void {
//游戲內(nèi)自定義分享按鈕
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)
}
微信分享主要使用了shareAppMessage()方法,具體參數(shù)可參看微信開發(fā)平臺.
class WxgamePlatform {
shareAppMessage() {
return new Promise((resolve, reject) => {
wx.shareAppMessage({
title: '轉(zhuǎn)發(fā)標(biāo)題',
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(){}
}
private async runGame() {
platform.createGameClubButton();
}
使用方法createGameClubButton().查看參看微信平臺
class WxgamePlatform {
wx.createGameClubButton({
icon: 'green',
style: {
left: 200,
top: 626,
width: 40,
height: 40
}
})
openDataContext = new WxgameOpenDataContext();
以上是微信小游戲四種常見功能的實現(xiàn)方法,希望對您有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。