如果不知道egret怎麼調用微信小程式API,請看Egret微信小遊戲API調用
該章最終效果如下:
一、在platform.ts中新增兩個函數share和init
/**
* 平台資料接口。
* 由于每款遊戲通常需要釋出到多個平台上,是以提取出一個統一的接口用于開發者擷取平台資料資訊
* 推薦開發者通過這種方式封裝平台邏輯,以保證整體結構的穩定
* 由于不同平台的接口形式各有不同,白鹭推薦開發者将所有接口封裝為基于 Promise 的異步形式
*/
declare interface Platform {
getUserInfo(): Promise<any>;
login(): Promise<any>;
share(): Promise<any>;
init(): Promise<any>;
}
class DebugPlatform implements Platform {
async getUserInfo() {
return { nickName: "username" }
}
async login() {
}
async share(){
}
async init() {
}
}
if (!window.platform) {
window.platform = new DebugPlatform();
}
declare let platform: Platform;
declare interface Window {
platform: Platform
}
init()方法是打開微信分享,同時也可以将其他要初始化的操作寫入到該函數中。
share() 方法實作分享功能。
二、在platform.js檔案中添加兩個方法init()和share()
/**
* 請在白鹭引擎的Main.ts中調用 platform.login() 方法調用至此處。
*/
class WxgamePlatform {
// 之前的代碼***
init() {
wx.showShareMenu();
}
share() {
wx.shareAppMessage({
title: "share test",
imageUrl: "http://pic.qiantucdn.com/58pic/22/06/55/57b2d98e109c6_1024.jpg!/fw/1024/watermark/url/L2ltYWdlcy93YXRlcm1hcmsvZGF0dS5wbmc=/repeat/true/crop/0x1024a0a0",
query: ""
});
}
// 之後的代碼
}
三、我們在main.ts中寫一個按鈕,點選後調用分享
/**
* 建立遊戲場景
* Create a game scene
*/
private createGameScene(userInfo: any) {
let bg: eui.Rect = new eui.Rect();
this.addChild(bg);
bg.width = this.stage.width;
bg.height = this.stage.height;
bg.fillColor = ;
let avatar: eui.Image = new eui.Image();
avatar.x = ;
avatar.y = ;
avatar.width = ;
avatar.height = ;
avatar.source = userInfo.avatarUrl;
this.addChild(avatar);
let nickName: eui.Label = new eui.Label();
nickName.x = ;
nickName.y = ;
nickName.textColor = ;
nickName.text = userInfo.nickName;
this.addChild(nickName);
let shareGroup: eui.Group = new eui.Group();
shareGroup.width = ;
shareGroup.height = ;
shareGroup.x = ;
shareGroup.y = ;
this.addChild(shareGroup);
let shareLabel: eui.Label = new eui.Label();
shareLabel.text = "share";
shareLabel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.share, this);
shareLabel.left = ;
shareLabel.top = ;
shareLabel.bottom = ;
shareLabel.right = ;
shareLabel.textColor = ;
let shareRect: eui.Rect = new eui.Rect();
shareRect.left = ;
shareRect.top = ;
shareRect.bottom = ;
shareRect.right = ;
shareRect.fillColor = ;
shareGroup.addChild(shareRect);
shareGroup.addChild(shareLabel);
}
private share() {
console.log("share");
platform.share();
}
當然你在測試的時候需要用一個可以使用的微信小遊戲ID,就可以分享發送給朋友。