当前位置: 首页 > Web前端 > HTML5

微信小游戏API调用Egret

时间:2023-04-06 00:05:45 HTML5

野子电竞数据官网改版https://www.xxe.io/全新登场在Egret中可以直接调用微信小游戏的API,在Egret中添加平台代码,可以调用各个平台的API。新建项目会在src文件夹下找到一个Platform.ts文件,如果没有请自己创建。/**平台数据接口。由于每个游戏通常需要在多个平台上发布,因此提炼出一个统一的接口供开发者获取平台数据信息。建议开发者通过这种方式封装平台逻辑,保证整体架构的稳定性。由于不同平台的接口形式不同。Egret建议开发者基于Promise*/declareinterfacePlatform{getUserInfo():Promise;login():Promise}将所有接口封装成异步形式}asynclogin(){}}if(!window.platform){window.platform=newDebugPlatform();}declareletplatform:Platform;declareinterfaceWindow{platform:Platform}12345678910111213141516171819202122232425并将代码复制到Platform.ts中。2、打包发布微信项目,在微信开发者工具中打开,可以看到platform.js文件,如果没有请新建一个platform.js文件。微信的其他接口也要写在这个文件里,比如分享,微信点击进入等。/**这里请调用EgretEngine的Main.ts中的platform.login()方法调用。*/classWxgamePlatform{name='wxgame'login(){returnnewPromise((resolve,reject)=>{wx.login({success:(res)=>{resolve(res)}})})}getUserInfo(){returnnewPromise((resolve,reject)=>{wx.getUserInfo({withCredentials:true,success:function(res){varuserInfo=res.userInfovarnickName=userInfo.nickNamevaravatarUrl=userInfo.avatarUrlvargender=userInfo.gender//性别0:未知、1:男、2:女varprovince=userInfo.provincevarcity=userInfo.cityvarcountry=userInfo.countryresolve(userInfo);}})})}openDataContext=newWxgameOpenDataContext();12345678910111213141516171819}classWxgameOpenDataContext{createDisplayObject(type,width,height){constbitmapdata=newegret.BitmapData(sharedCanvas);bitmapdata.$deleteSource=false;consttexture=newegret.Texture();texture._setBitmapData(位图数据);const位图=newegret.Bitmap(texture);位图.width=宽度;位图.height=高度;egret.startTick((timeStarmp)=>{egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);bitmapdata.webGLTexture=null;returnfalse;},this);returnbitmap;}postMessage(data){constopenDataContext=wx.getOpenDataContext();openDataContext.postMessage(data);}123456789101112}window.platform=newWxgamePlatform();1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162Platform声明了平台函数,在egretItcanbecalleddirectlyineachplatform,andthecodecanbeimplementedineachplatform.3.在egret中调用login,获取微信用户信息。在main.ts中写入如下代码。awaitplatform.login()调用登录接口,constuserInfo=awaitplatform.getUserInfo();获取微信用户信息并返回用户信息,异步等待调用,返回调用结果并执行下一步。classMainextendsegret.DisplayObjectContainer{publicconstructor(){super();this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);}privateonAddToStage(event:egret.Event){this.runGame().catch(e=>{console.log(e);})}privateasyncrunGame(){awaitplatform.login();constuserInfo=awaitplatform.getUserInfo();this.createGameScene(userInfo);}私有文本字段:egret.TextField;/**创建游戏场景创建游戏场景*/privatecreateGameScene(userInfo:any){letbg:eui.Rect=neweui.Rect();this.addChild(bg);bg.width=this.stage.width;bg.height=this.stage.height;bg.fillColor=0xF8F8F8;letavatar:eui.Image=neweui.Image();avatar.x=100;avatar.y=100;avatar.width=120;avatar.height=120;avatar.source=userInfo.avatarUrl;this.addChild(avatar);letnickName:eui.Label=neweui.Label();nickName.x=100;nickName.y=250;nickName.textColor=0xff0000;nickName.text=userInfo.nickName;this.addChild(nickName);}12345678910111213141516171819202122232425262728293031323334353637383940414243444546}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849打包微信工程,并在开发者工具打开运行获取到微信用户信息。———————————————