ExploreNew.js 13.6 KB
var Common = require('Common');
var Network = require('Network');
var TVFocus = require('TVFocus');
var FocusInfo = require('FocusInfo');
var TVCanvas = require('TVCanvas');
var TVScrollParameter = require('TVScrollParameter');
var ListView = require('ListView');
var ListCell = require('ListCell');
var xmlToJSON = require('xmlToJSON');

cc.Class({
    extends: TVCanvas,

    properties: {
        PFB_TYPE: {
            default: null,
            type: cc.Prefab,
        },
        PFB_POP: {
            default: null,
            type: cc.Prefab,
        },
    },

    start: function () {
        this.bg_long.getComponent(cc.Animation).play();        //播放动画

        // this.schedule(()=>{
        //     this.bg_long.getComponent(cc.Animation).pause();        //暂停播放动画
        //     this.hero.getComponent(dragonBones.ArmatureDisplay)._armature.animation.stop();
        // },5);
        this.scheduleOnce(() => {           //指定0让回调函数在下一帧立即执行(推荐位初始化图片后,需等待下一帧操作)
            this.bg_long.getComponent(cc.Animation).pause();
        }, 0);
    },

    onLoad: function () {
        this._super();

        this._aFocusTargets[1] = [];    //弹窗焦点
        this._iSceneStatus = 0;   //焦点层级
        this._remindString = this.node.getChildByName("Remind").getComponent(cc.Label);
        this._progressString = cc.find("Progress/progress", this.node).getComponent(cc.Label);
        this._progressBg = cc.find("Progress/scrollViewBg", this.node);
        this._progressBar = cc.find("Progress/scrollViewBg/scrollViewBar", this.node);

        this.totalTask = 0;   //任务进度,根据配置表,每个
        this.bg_long = this.node.getChildByName("Bg_long");
        // this.bg_long.getComponent(cc.Animation).play();        //播放动画
        // this.bg_long.getComponent(cc.Animation).stop();        //暂停播放动画

        this.hero = this.node.getChildByName("Hero");
        // this.hero.getComponent(dragonBones.ArmatureDisplay).playAnimation();
        // this.hero.getComponent(dragonBones.ArmatureDisplay)._armature.animation.stop();

        var self = this;
        //动态加载背景图,需指定类型(需要拼接图片),指定尺寸(目前使用的是7280*720)           
        cc.loader.loadRes("game/explore_plus", cc.Texture2D, function (err, texture) {
            self.bg_long.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
        });

        //显示向右的动作指引
        let actionUp = cc.moveTo(0.3, cc.v2(480, 0));
        let actionDown = cc.moveTo(0.3, cc.v2(500, 0));
        this.node.getChildByName("Next").runAction(cc.repeatForever(cc.sequence(actionUp, actionDown)));

        //分类框框
        this._nodeDialogListWrapper = cc.find("PopWindow/DialogListArea/DialogListWrapper", this.node);
        this._nodeDialogList = cc.find("DialogList", this._nodeDialogListWrapper);
        // this._nodeDialogList.addComponent(TVScrollParameter);

        cc.loader.loadRes("config/explore_config", function (err, str) {
            var x2js = new xmlToJSON();
            self._exploreConfig = x2js.xml_str2json(str);     //解析xml
            cc.log("解析后xml------------>" + JSON.stringify(self._exploreConfig));
            // cc.log("解析后xml------------>"+result.root.explore[0]["_choice2"]);
        });

        this.initFocus();
    },

    initFocus: function () {
        var fiNext = this.node.getChildByName("Next").addComponent(FocusInfo);//
        this._aFocusTargets[0]['to_next'] = this.node.getChildByName("Next");
        fiNext.init(
            null, true
        );

        var fiClose = this.node.getChildByName("BackAScene").addComponent(FocusInfo);//
        this._aFocusTargets[0]['to_back_scene'] = this.node.getChildByName("BackAScene");
        fiClose.init(
            '', true
        );

        var fiDiloaClose = cc.find("PopWindow/Close",this.node).addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_close_popwindow'] = cc.find("PopWindow/Close",this.node);
        fiDiloaClose.init(
            '{"click": [{"action": "Close"}]}', true
        );

        var nodeFocus = new cc.Node('nodeFocus');
        this.node.addChild(nodeFocus, 10);
        this._cFocus = this.node.getChildByName('nodeFocus').addComponent(TVFocus);
        this._cFocus.init('focusContainer', this,
            fiNext.getComponent(FocusInfo),
            Common.SCREEN_WIDTH, Common.SCREEN_HEIGHT, 6, 4, 1.0);
        // this._cFocus.hide();        //所有焦点都是隐藏的

    },

    keyDownDirection: function (Direct) {
        var fiFocusTarget = null;
        var fiCurrentFocus = this._fiCurrentFocus;
        var oScrollParameter = null;

        fiFocusTarget = this._cFocus.findTarget(fiCurrentFocus, this._aFocusTargets, this._iSceneStatus, Direct);
        fiFocusTarget = this.checkFocusTarget(fiFocusTarget);
        if (!fiFocusTarget) { return; }
        // this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Direct, null, oScrollParameter);
        // this.scheduleOnce(() => {           //指定0让回调函数在下一帧立即执行(推荐位初始化图片后,需等待下一帧操作)
        this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Direct, null, oScrollParameter);
        // }, 0.5);
    },

    checkFocusTarget: function (fiFocusTarget) {
        return fiFocusTarget;
    },

    onKeyDown: function (event) {

        switch (event.keyCode) {
            case cc.macro.KEY.up:
            case Common.ANDROID_KEY.up:
                this.keyDownDirection(Common.MOVE_DIRECTION_UP);
                break;
            case cc.macro.KEY.right:
            case Common.ANDROID_KEY.right:
                this.keyDownDirection(Common.MOVE_DIRECTION_RIGHT);
                break;
            case cc.macro.KEY.down:
            case Common.ANDROID_KEY.down:
                this.keyDownDirection(Common.MOVE_DIRECTION_DOWN);
                break;
            case cc.macro.KEY.left:
            case Common.ANDROID_KEY.left:
                this.keyDownDirection(Common.MOVE_DIRECTION_LEFT);
                break;
            case cc.macro.KEY.enter:
            case cc.macro.KEY.space:
            case Common.ANDROID_KEY.enter:
                if (0 == this._fiCurrentFocus.node.getName().indexOf('Next')) {
                    this.nextTask();
                    break;
                }else if(0 == this._fiCurrentFocus.node.getName().indexOf('BackAScene')){
                    this.backAScene();
                }
                this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK); 
                break;
            case cc.macro.KEY.backspace:
            case Common.ANDROID_KEY.back:
                this.backAScene();
                break;
        }
    },

    /**
     * 点击触发下一个事件
     */
    nextTask: function () {
        // cc.log("播放事件");
        if (this.totalTask == 5) {
            this._remindString.string = "恭喜,您已经通关!";
            this._remindString.node.active = true;
            this.scheduleOnce(() => {
                this._remindString.node.active = false;
            }, 3);
            return ;
        }
        this.bg_long.getComponent(cc.Animation).resume();        //播放动画
        this.hero.getComponent(dragonBones.ArmatureDisplay).playAnimation();

        this.scheduleOnce(() => {
            this.bg_long.getComponent(cc.Animation).pause();        //暂停播放动画
            this.hero.getComponent(dragonBones.ArmatureDisplay)._armature.animation.stop();

            let randomNum = this.random(1, 7);
            cc.log("随机数-------->" + randomNum);
            var exploreObj = this._exploreConfig.root.explore[randomNum - 1];
            // cc.log("对象----->" + JSON.stringify(exploreObj));
            var type = exploreObj["_type"];
            if (type == 1) {                //选择事件,需要用户进行对话框选择,进入相应的功能
                this.totalTask += 1;
                this.node.getChildByName("Animal").active = true;
                this._nodeDialogList.removeAllChildren();   //先清理
                for (let i = 0; i < 2; i++) {
                    let nodeDialogCell = cc.instantiate(this.PFB_TYPE);
                    nodeDialogCell.y = -nodeDialogCell.height / 2 - i * (nodeDialogCell.height + 20);
                    this._nodeDialogList.addChild(nodeDialogCell, 10, "DialogCell_" + i);
                    //准备焦点坐标
                    let fiDialogBlock = nodeDialogCell.addComponent(FocusInfo);
                    fiDialogBlock.init('{"click": [{"action": "ChangeScene","parameters": {"sceneName":"sceneProgram"}}]}',
                        true, null, null, 1.0);
                    this._aFocusTargets[1]['dialog_list_cell_' + i] = nodeDialogCell;

                    if (i == 0) {
                        //让小动物显示1秒再弹窗
                        this.scheduleOnce(() => {
                            this._iSceneStatus = 1;
                            var fiFocusTarget = this._nodeDialogList.getChildByName("DialogCell_0").getComponent(FocusInfo);
                            this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_LEFT, null, null);

                            this.node.getChildByName("PopWindow").active = true;
                            this.node.getChildByName("PopWindow").zIndex = 10;
                            this.node.getChildByName("Next").zIndex = 0;
                        }, 1);
                    }
                }

            } else if (type == 2) {          //奖励事件,触发后直接获得奖励
                this.totalTask += 1;
                this._remindString.string = "恭喜您发现了宝箱!";
                this._remindString.node.active = true;
                this._remindString.node.getChildByName("Box").active=true;
                this.scheduleOnce(() => {
                    this._remindString.node.getChildByName("Box").active=false;
                    this._remindString.node.active = false;
                }, 2);

                let popItem=cc.instantiate(this.PFB_POP);
                this.node.addChild(popItem,100);
                this.node.anchorX=0;        //解决飘窗显示位置不在屏幕中央的问题
                this.node.anchorY=0;
                function fadeOutBack(){
                    popItem.destroy();   //用完就销毁
                    cc.log("处理飘窗彻底消失后的逻辑。。。。。。");
                };
                let fadeOut=cc.fadeOut(1.0);
                let moveBy=cc.moveBy(1,cc.v2(0,200));
                popItem.runAction(moveBy);
                popItem.runAction(cc.sequence(fadeOut,cc.callFunc(fadeOutBack)));
            } else if (type == 3) {          //倒退事件,触发后地图探索进度减少
                this.totalTask += 1;
                this._remindString.string = "找到了捷径,进度增加20%";
                this._remindString.node.active = true;
                this.scheduleOnce(() => {
                    this._remindString.node.active = false;
                }, 2);
            } else if (type == 4) {          //前进事件,触发后地图探索进度增加
                if(this.totalTask>0) this.totalTask -= 1;
                this._remindString.string = "迷路了,进度减少20%";
                this._remindString.node.active = true;
                this.scheduleOnce(() => {
                    this._remindString.node.active = false;
                }, 2);
            }
            cc.log("----------------" + this.totalTask);
            //进度
            if (this.totalTask > 5) {
                this._remindString.string = "恭喜,您已经通关!";
                this._remindString.node.active = true;
                this.scheduleOnce(() => {
                    this._remindString.node.active = false;
                }, 2);
            }
            else if (this.totalTask >= 0) {
                this._progressString.string = 20 * this.totalTask + "%";
                this._progressBar.width = 20 * this.totalTask / 100 * 800;
                cc.log("进度-----》" + this._progressBar.width);
            }
        }, 5);
    },

    /**
        产生随机整数,包含下限值,包括上限值
        @param {Number} lower 下限
        @param {Number} upper 上限
        @return {Number} 返回在下限到上限之间的一个随机整数
    */
    random: function (lower, upper) {
        return Math.floor(Math.random() * (upper - lower + 1)) + lower;
    },

    doCurrentFocusTVLinkAction: function (strAction) {
        let strTVLink = this._fiCurrentFocus.getTVLink();
        var joTVLink = null;
        try {
            joTVLink = JSON.parse(strTVLink);
            let jaOperationList = joTVLink.click;
            for (let i = 0; i < jaOperationList.length; i++) {
                switch (jaOperationList[i].action) {
                    case 'Close':
                        this.node.getChildByName("Animal").active = false;
                        this.node.getChildByName("PopWindow").active = false;
                        this._iSceneStatus = 0;
                        var fiFocusTarget = this.node.getChildByName("Next").getComponent(FocusInfo);
                        this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_RIGHT, null, null);
                        break;
                    default:
                        this.doTVLinkAction(jaOperationList[i]);
                        break;
                }
            }
        } catch (error) {
            cc.log("runTVLinkAction Exception..." + error);
        }
    },


});