微信小遊戲 在節點按鈕button上建立一個透明的按鈕,等待使用者點選授權,授權後隱藏或銷燬按鈕
//在節點按鈕button上建立一個透明的按鈕,等待使用者點選授權,授權後隱藏或銷燬按鈕 createAuthorizeBtn:function(btnNode){
let btnSize = cc.size(btnNode.width+10,btnNode.height+10); let frameSize = cc.view.getFrameSize(); let winSize = cc.director.getWinSize();
// console.log("winSize: ",winSize); // console.log("frameSize: ",frameSize);
//適配不同機型來建立微信授權按鈕 let left = (winSize.width*0.5+btnNode.x-btnSize.width*0.5)/winSize.width*frameSize.width; let top = (winSize.height*0.5-btnNode.y-btnSize.height*0.5)/winSize.height*frameSize.height; let width = btnSize.width/winSize.width*frameSize.width; let height = btnSize.height/winSize.height*frameSize.height;
// console.log("button pos: ",cc.v2(left,top)); // console.log("button size: ",cc.size(width,height));
let self = this; self.btnAuthorize = wx.createUserInfoButton({ type:"text", text:"", style:{ left:left, top:top, width:width, height:height, lineHeight:0, backgroundColor:"",//透明 color:"#ffffff", textAlign:'center', fontSize:16, borderRadius:4, },
});
self.btnAuthorize.onTap((res)=>{ console.log("onTap res: " + res); if(res.userInfo){ // console.log("wxLogin Auth success!"); wx.showToast({title:"授權成功"}); //授權成功 隱藏或銷燬按鈕 cc.find("Canvas").getComponent("StartMainScene").bAuthorizeSuccess = true; cc.find("Canvas").getComponent("StartMainScene").btnAuthorize.hide();//隱藏按鈕,或btnAuthorize.destroy();
wx.getUserInfo({ success:function(res){ cc.log(res.userInfo)
cc.find("Canvas").getComponent("StartMainScene").userInfo = res.userInfo;
cc.loader.load({url:res.userInfo.avatarUrl,type:"jpg"}, function(err, texture){ cc.find("Canvas/playerphoto").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture); }); } }); } else{ // console.log("wxLogin Auth fail"); wx.showToast({title:"授權失敗"}); } }); },