egret php交互,微信小游戏API调用Egret
野子电竞数据官网改版https://www.xxe.io/全新登场
在Egret中是可以直接调用微信小游戏的API的,Egret中添加了平台代码,可以调用各个平台API。
新建工程会发现在src文件夹中会包含一个Platform.ts的文件,如果没有请创建。
/**
平台数据接口。
由于每款游戏通常需要发布到多个平台上,所以提取出一个统一的接口用于开发者获取平台数据信息
推荐开发者通过这种方式封装平台逻辑,以保证整体结构的稳定
由于不同平台的接口形式各有不同,白鹭推荐开发者将所有接口封装为基于 Promise 的异步形式
*/
declare interface Platform {
getUserInfo(): Promise;
login(): Promise
}
class DebugPlatform implements Platform {
async getUserInfo() {
return { nickName: “username” }
}
async login() {
}
}
if (!window.platform) {
window.platform = new DebugPlatform();
}
declare let platform: Platform;
declare interface Window {
platform: Platform
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
并将代码拷贝到Platform.ts中。
二、打包发布微信工程,并在微信开发者工具中打开,可以看到platform.js文件,若没有,请创建platform.js文件。微信的其他接口也要写到该文件中,分享、微信点击进入等。
/**
请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
*/
class WxgamePlatform {
name = ‘wxgame’
login() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
resolve(res)
}
})
})
}
getUserInfo() {
return new Promise((resolve, reject) => {
wx.getUserInfo({
withCredentials: true,
success: function (res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //性别 0:未知、1:男、2:女
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
resolve(userInfo);
}
})
})
}
openDataContext = new WxgameOpenDataContext();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
}
class WxgameOpenDataContext {
createDisplayObject(type,width,height){
const bitmapdata = new egret.BitmapData(sharedCanvas);
bitmapdata.$deleteSource = false;
const texture = new egret.Texture();
texture._setBitmapData(bitmapdata);
const bitmap = new egret.Bitmap(texture);
bitmap.width = width;
bitmap.height = height;
egret.startTick((timeStarmp) => {
egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
bitmapdata.webGLTexture = null;
return false;
}, this);
return bitmap;
}
postMessage(data){
const openDataContext = wx.getOpenDataContext();
openDataContext.postMessage(data);
}
1
2
3
4
5
6
7
8
9
10
11
12
}
window.platform = new WxgamePlatform();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Platform声明了平台函数,在egret中可以直接调用,在各个平台中实现代码即可。
三、在egret中调用登陆,并获取微信用户信息。在main.ts中写入如下代码。
await platform.login()调用登陆API,
const userInfo = await platform.getUserInfo();获取微信用户信息,并返回用户信息,
await 异步调用,返回调用结果在执行下一步。
class Main extends egret.DisplayObjectContainer {
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
}
private onAddToStage(event: egret.Event) {
this.runGame().catch(e => {
console.log(e);
})
}
private async runGame() {
await platform.login();
const userInfo = await platform.getUserInfo();
this.createGameScene(userInfo);
}
private textfield: egret.TextField;
/**
创建游戏场景
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 = 0xF8F8F8;
let avatar: eui.Image = new eui.Image();
avatar.x = 100;
avatar.y = 100;
avatar.width = 120;
avatar.height = 120;
avatar.source = userInfo.avatarUrl;
this.addChild(avatar);
let nickName: eui.Label = new eui.Label();
nickName.x = 100;
nickName.y = 250;
nickName.textColor = 0xff0000;
nickName.text = userInfo.nickName;
this.addChild(nickName);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
打包微信工程,并在开发者工具打开。运行获取到微信用户信息。
————————————————
egret php交互,微信小游戏API调用Egret相关推荐
- 微信小游戏API (doing)
微信小游戏API (doing) 1. 登录 wx.login({success: function(response) {if (response.code) {wx.request({url: ' ...
- 白鹭引擎egret打包成微信小游戏
1.导入项目以后打开文件egretproperties.json修改target 下面的current的web为wxgame r 然后点开打包 在这里填写微信小游戏id以及命名,打包成功以后,即可用微 ...
- JAVA开发(后端):微信小程序API调用详细分析及步骤
关键词:微信登录.统一下单(支付).统一下单通知(回调).统一下单查询.企业付款至零钱.支付查询.获取ACCESS_Token.获取小程序二维码 因为做项目涉及到微信这些接口的调用,尽管看了很多博客, ...
- 微信小程序——api调用(糗事百科例子)
糗事百科api分析 糗事百科 http://m2.qiushibaike.com/article/list/{type}?type=refresh&page={page}&count= ...
- 微信小游戏 腾讯课堂《白鹭教育 - 成语大挑战小游戏开发》学习笔记
腾讯课堂<白鹭教育 - 成语大挑战小游戏开发>:https://ke.qq.com/course/287266 学习笔记 首先这个视频课程简单介绍了一个小游戏的制作,包括新建项目.适配模式 ...
- 白鹭引擎开发微信小游戏: API 调用教程文档
如何在 Egret 中调用小游戏 API 在 Egret 中是可以直接调用小游戏的 API 的,这篇文档简要介绍如何在 Egret 中使用小游戏的 API. 新增 platform.ts 文件 在 ...
- egret微信小游戏相关
文章目录 官方文档 开放数据域 微信登录授权登录,获取微信用户信息,分享小游戏 微信排行榜 微信排行榜数据获取 微信好友排行榜绘制 主域名发送消息,共享域接受消息 其他注意事项 官方文档 微信小游戏快 ...
- 微信小游戏接入遇到的坑
微信小游戏接入遇到的坑 1.微信web开发工具必须安装到C盘,才能被egret wing自动调取. 2.exml文件不能放在src文件夹,必现放在resource文件夹 3.egret Launche ...
- 微信小游戏|开放数据域的写与读
本文来自Cocos官方论坛,感谢「达闻西」的分享! 1. 引言 本来是自己写的笔记,想想还是发出来把,能让不少新手少走弯路,毕竟自己也是摸索了好久!以图片的形式,让大家更直观点! 微信小游戏API官方 ...
最新文章
- [转]thinkphp 模板显示display和assign的用法
- VC中使用全局变量的2种办法及防错措施
- poj 3061(尺取法)
- 2021-11-09类作为成员变量类型
- 那些年我用过的开源软件、框架
- 使用Select.HtmlToPdf 把html内容生成pdf文件
- Decoder is not a @Sharable handler, so can't be added or removed multiple times
- SQL索引管理器——用于SQL Server和Azure上的索引维护的免费GUI工具
- 跟着百度学PHP[4]OOP面对对象编程-16-switch逻辑就语句
- 子级Repeater获取 父级Repeater 中的值
- html小写罗马字符怎么写,如何在 LATEX 中插入大小写的罗马字符
- 你曾后悔进入 IT 行业吗?
- 查找mysql.sock_找不到mysql.sock,我在哪里可以找到它?
- H5 App实现热更新,不需要重新安装app
- 机器学习思维导图(更新中)
- 文件上传绕waf(waf拦不住我)
- Android Audio 音频输出设备切换
- 64位32位 java 浏览器_Java编程语言下 Selenium 驱动各个浏览器代码
- 98-微服务项目的编写(下篇)
- 6,Matlab轨迹生成