微信排行榜功能

旧巷老猫 提交于 2019-11-30 10:44:47

我参考网上的写的


    private bgGroup: eui.Group = null;
    public bitmap: egret.Bitmap = null;
    private isRankClick: boolean = false;
    private rankingListMask: egret.Shape;

    private async initWX() {
        egret.log("initWX 1");
        this.bgGroup = new eui.Group();
        this.bgGroup.width = 720;
        this.bgGroup.height = 1280;
        this.addChild(this.bgGroup);

        let bgImg: egret.Bitmap = new egret.Bitmap();
        bgImg.texture = RES.getRes(ResImgDefine.img_loading_bg_jpg);
        this.bgGroup.addChild(bgImg);

        let btnLogin: egret.Bitmap = new egret.Bitmap();
        btnLogin.texture = RES.getRes(ResImgDefine.btn_ok_png);
        this.bgGroup.addChild(btnLogin);
        btnLogin.x = (GDatas.getStageWidth() - btnLogin.width) / 2;
        btnLogin.y = (GDatas.getStageHeight() - btnLogin.height) / 2;

        let label: eui.Label = new eui.Label();
        label.horizontalCenter = 0.5;
        label.verticalCenter = 0.5;
        label.verticalAlign = egret.VerticalAlign.MIDDLE;

        label.bold = true;
        label.size = 40;
        label.textAlign = egret.HorizontalAlign.CENTER;
        label.$setWidth(btnLogin.width);
        label.$setHeight(btnLogin.height);
        label.$setBackgroundColor(0x00ff00);
        label.$setTextColor(0x000000);
        label.$setText("开始");
        this.bgGroup.addChild(label);
        label.x = btnLogin.x;
        label.y = btnLogin.y;

        GDatas.platformType = EPlatformType.h5;

        let sign = await platform.getPlatformSign();
        if (sign) {
            let plat: string = sign["platform"]
            egret.log("is plat = ", plat);
            if (plat === "wx") {
                GDatas.platformType = EPlatformType.wx;

                // GDatas.userInfo = await platform.getUserInfo();
                // egret.log("GameEntry:: userInfo = ", GDatas.userInfo);
                let loginRes = await platform.login();
                // loginRes =  {errMsg: "login:ok", code: "043NGnW70DszQF1nnCW700uHW70NGnWs"}
                egret.log("loginRes = ", loginRes);

                this.showRank();
            }
        }

        // App.DisplayUtils.removeFromParent(btnLogin);
        // App.DisplayUtils.removeFromParent(bgImg);
        // App.DisplayUtils.removeFromParent(label);
        // App.DisplayUtils.removeFromParent(this.bgGroup);
        // this.bgGroup = null;
        // this.enterGame();
    }

    private showRank() {

        this.bgGroup.touchEnabled = true;
        this.bgGroup.touchChildren = false;
        this.bgGroup.addEventListener(egret.TouchEvent.TOUCH_TAP, this.obBtnRankingClick, this);
        platform.openDataContext.postMessage({
            command: 'loadRes'
        });
    }


    //显示微信排行榜

    public obBtnRankingClick(e: egret.TouchEvent) {
        console.log("点击排行榜");
        let plat: any = window.platform;
        egret.log("platform = ", platform);
        let context = platform.openDataContext;
        if (!context) {
            egret.error("Main::showRank context");
            return;
        }

        if (!this.isRankClick) {

            //处理遮罩,避免开放域数据影响主域

            this.rankingListMask = new egret.Shape();
            this.rankingListMask.graphics.beginFill(0x000000, 1);

            this.rankingListMask.graphics.drawRect(0, 0, this.stage.width, this.stage.height);
            this.rankingListMask.graphics.endFill();
            this.rankingListMask.alpha = 0.4;

            //设置为true,以免触摸到下面的按钮

            this.rankingListMask.touchEnabled = true;

            this.addChildAt(this.rankingListMask, 999);

            //让排行榜按钮显示在容器内

            //TODO: 这里
            // this.addChild(this.btn_ranking);


            //显示开放域数据

            this.bitmap = plat.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);
            this.addChild(this.bitmap);
            //主域向子域发送数据
            plat.openDataContext.postMessage({
                isRanking: this.isRankClick,
                text: "egret",
                year: (new Date()).getFullYear(),
                command: "open"
            });

            this.isRankClick = true;
        } else {
            this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);
            this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);

            this.isRankClick = false;
            plat.openDataContext.postMessage({
                isRanking: this.isRankClick,
                text: "egret",
                year: (new Date()).getFullYear(),
                command: "close"
            });
        }
    }

 

附参考:https://www.mmzsblog.cn/articles/2019/09/10/1568118032212.html

一、调用默认排行榜

1、首先我们需要了解:

  • 1。白鹭开发文档(开放数据域):http://developer.egret.com/cn/github/egret-docs/Engine2D/minigame/openDataContext/index.html

  • 2.微信小游戏开发文档(关系链数据):https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/open-data.html

  • 核心代码:

  • class Main extends eui.UILayer {
    
        private bitmap: egret.Bitmap;
        private isdisplay = false;
        private rankBtn: eui.Button;
        private rankingListMask: egret.Shape;
    
        protected createChildren(): void {
            super.createChildren();
    
            egret.lifecycle.addLifecycleListener((context) => {
                // custom lifecycle plugin
                context.onUpdate = () => {
    
                }
            })
    
            egret.lifecycle.onPause = () => {
                egret.ticker.pause();
            }
    
            egret.lifecycle.onResume = () => {
                egret.ticker.resume();
            }
    
            //inject the custom material parser
            //注入自定义的素材解析器
            let assetAdapter = new AssetAdapter();
            egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
            egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
    
    
            this.runGame().catch(e => {
                console.log(e);
            })
        }
    
        private async runGame() {
            await this.loadResource()
            this.createGameScene();
            // const result = await RES.getResAsync("description_json")
            // this.startAnimation(result);
            await platform.login();
            await platform.showShareMenu();
            const userInfo = await platform.getUserInfo();
            console.log(userInfo);
            // await platform.showShareMenu();
        }
    
        private async loadResource() {
            try {
                const loadingView = new LoadingUI();
                this.stage.addChild(loadingView);
                await RES.loadConfig("resource/default.res.json", "resource/");
                await this.loadTheme();
                await RES.loadGroup("preload", 0, loadingView);
                this.stage.removeChild(loadingView);
            }
            catch (e) {
                console.error(e);
            }
        }
    
        private loadTheme() {
            return new Promise((resolve, reject) => {
                // load skin theme configuration file, you can manually modify the file. And replace the default skin.
                //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
                let theme = new eui.Theme("resource/default.thm.json", this.stage);
                theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                    resolve();
                }, this);
    
            })
        }
            /**
         * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
         * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
         */
        private createBitmapByName(name: string): egret.Bitmap {
            let result = new egret.Bitmap();
            let texture: egret.Texture = RES.getRes(name);
            result.texture = texture;
            return result;
        }
        /**
         * 描述文件加载成功,开始播放动画
         * Description file loading is successful, start to play the animation
         */
        private startAnimation(result: Array<any>): void {
            // let parser = new egret.HtmlTextParser();
    
            // let textflowArr = result.map(text => parser.parse(text));
            // let textfield = this.textfield;
            // let count = -1;
            // let change = () => {
            //     count++;
            //     if (count >= textflowArr.length) {
            //         count = 0;
            //     }
            //     let textFlow = textflowArr[count];
    
            //     // 切换描述内容
            //     // Switch to described content
            //     textfield.textFlow = textFlow;
            //     let tw = egret.Tween.get(textfield);
            //     tw.to({ "alpha": 1 }, 200);
            //     tw.wait(2000);
            //     tw.to({ "alpha": 0 }, 200);
            //     tw.call(change, this);
            // };
    
            // change();
        }
        private textfield: egret.TextField;
        /**
         * 创建场景界面
         * Create scene interface
         */
        protected createGameScene(): void {
            let sky = this.createBitmapByName("bg_jpg");
            this.addChild(sky);
            let stageW = this.stage.stageWidth;
            let stageH = this.stage.stageHeight;
            sky.width = stageW;
            sky.height = stageH;
            //打开关闭排行榜按钮
            this.rankBtn = new eui.Button();
            this.rankBtn.label = "排行";
            this.rankBtn.x = 260;
            this.rankBtn.y = 750;
            this.addChild(this.rankBtn);
            this.rankBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickRankBtn, this);
            //加载资源
            const platform: any = window.platform;
            platform.openDataContext.postMessage({
                command: 'loadRes'
            });
    
        }
    
        private onClickRankBtn() {
            console.log('点击btnClose按钮');
            let platform: any = window.platform;
            if (this.isdisplay) {
                this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);
                this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);
                this.isdisplay = false;
                platform.openDataContext.postMessage({
                    isDisplay: this.isdisplay,
                    text: 'hello',
                    year: (new Date()).getFullYear(),
                    command: "close"
                });
            } else {
                //处理遮罩,避免开放数据域事件影响主域。
                this.rankingListMask = new egret.Shape();
                this.rankingListMask.graphics.beginFill(0x000000, 1);
                this.rankingListMask.graphics.drawRect(0, 0, this.stage.width, this.stage.height);
                this.rankingListMask.graphics.endFill();
                this.rankingListMask.alpha = 0.5;
                this.rankingListMask.touchEnabled = true;
                this.addChild(this.rankingListMask);
    
                //简单实现,打开这关闭使用一个按钮。
                this.addChild(this.rankBtn);
                //主要示例代码开始
                this.bitmap = platform.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);
                this.addChild(this.bitmap);
                //主域向子域发送自定义消息
                platform.openDataContext.postMessage({
                    isDisplay: this.isdisplay,
                    text: 'hello',
                    year: (new Date()).getFullYear(),
                    command: "open"
                });
                //主要示例代码结束            
                this.isdisplay = true;
            }
        }
    
    }
    

     

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!