微信小游戏 在节点按钮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:"授权失败"});
            }
        });
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值