Explore.js 5.02 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');

cc.Class({
    extends: TVCanvas,

    properties: {
        _speed: 1,
        _hero_start_x: 0,
        _hero_end_x: 3648,
        _hero_y: 0,
    },

    onLoad: function () {
        this._super();
        this._hero_start_x = 500;
        this.bg_1 = this.node.getChildByName("bg_1");
        this.bg_2 = this.node.getChildByName("bg_2");
        this.cur_bg = this.bg_1;
        this.testmap = this.node.getChildByName("testmap");

        this.animLayer = this.testmap.getComponent(cc.TiledMap).getLayer("Animal");
        this.hero = this.node.getChildByName("hero");
        this._hero_y = this.hero.y;
    },

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

        fiFocusTarget = this._cFocus.findTarget(fiCurrentFocus, this._aFocusTargets, 0, 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:
                this._hero_start_x += 100;
                this._speed = 1;
                // this.hero.getComponent(dragonBones.ArmatureDisplay).playAnimation("Animation1");
                // this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
                break;
            case cc.macro.KEY.backspace:
            case Common.ANDROID_KEY.back:
                this.backAScene();
                break;
        }
    },

    update: function (dt) {
        this.bg_1.x -= this._speed;
        this.bg_2.x -= this._speed;
        this.testmap.x -= this._speed;
        this._hero_start_x += this._speed;
        if (this.cur_bg.x <= -1820) {
            if (this.cur_bg == this.bg_2) {
                this.bg_2.x = this.bg_1.x + 1820;
                this.cur_bg = this.bg_1;
            } else {
                this.bg_1.x = this.bg_2.x + 1820;
                this.cur_bg = this.bg_2;
            }
        }

        // cc.log("hero位置--------》" + this._hero_start_x);
        var pos = this._getTilePos(cc.v2(this._hero_start_x, 4));
        // cc.log("位置----------》"+pos);
        if (this._hero_start_x < this._hero_end_x) {    //不能超过瓦片地图最长长度
            //判断gid是否为0,该地图位置就会出现障碍物
            var gid = this.animLayer.getTileGIDAt(pos);
            // cc.log("gid========" + gid);
            if (gid != 0) {
                this._speed = 0;
                this.hero.getComponent(dragonBones.ArmatureDisplay).playAnimation("");
            }
        }
    },

    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) {
                    default:
                        this.doTVLinkAction(jaOperationList[i]);
                        break;
                }
            }
        } catch (error) {
            cc.log("runTVLinkAction Exception..." + error);
        }
    },

    _getTilePos: function (posInPixel) {
        var mapSize = this.testmap.getContentSize();
        var tileSize = this.testmap.getComponent(cc.TiledMap).getTileSize();
        var x = Math.floor(posInPixel.x / tileSize.width);
        var y = Math.floor((mapSize.height - posInPixel.y) / tileSize.height);
        return cc.v2(x, y);
    },

});