導讀微信小程序,簡稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應用,它實現了應用“觸手可及”的夢想,用戶掃一掃或搜一下即可打開應用。小程序是一種不用下載就能使用的應用,也是一... 微信小程序,簡稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應用,它實現了應用“觸手可及”的夢想,用戶掃一掃或搜一下即可打開應用。小程序是一種不用下載就能使用的應用,也是一項門檻非常高的創新,經過將近兩年的發展,已經構造了新的小程序開發環境和開發者生態。 本篇文章給大家帶來的內容是關于微信小游戲中如何實現轉發&分享&獲取頭像&游戲圈四種功能,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。今天我們分享的菜鳥教程文檔將介紹開發微信小游戲四種常用功能的實現方法,期望能和開發者朋友們交流,非常歡迎大家給我們留言反饋。 這四種功能分別是: 獲取頭像功能 在Egret Wing和微信開發者工具里的配置 為實現以上四個功能,我們需要分別在Egret Wing(圖1,圖2)和微信開發者工具(圖3)里配置。 需要在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(){} }
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(); } 以上是微信小游戲四種常見功能的實現方法,希望對您有所幫助。 相關推薦: 以上就是微信小游戲中如何實現轉發&分享&獲取頭像&游戲圈四種功能的詳細內容,更多請關注php中文網其它相關文章! 小程序是一種不需要下載安裝即可使用的應用,它實現了應用“觸手可及”的夢想,用戶掃一掃或者搜一下即可打開應用。 |
溫馨提示:喜歡本站的話,請收藏一下本站!