// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

        Hero: {
            // ATTRIBUTES:
            default: null,        // The default value will be used only when the component attaching
                                  // to a node for the first time
            type: cc.Node, // optional, default is typeof default
            serializable: true,   // optional, default is true
        },

        layout : {
            default: null,        // The default value will be used only when the component attaching
            // to a node for the first time
            type: cc.Node
        },


        tagDistance : false,

        //0 跳远 1 标枪
        type : 0,


        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () {
        this.count = this.layout.getChildByName("layoutcount").getChildByName("count");
        this.layoutcount = this.layout.getChildByName("layoutcount");
        this.updateNum = 0;
        this.layoutcount.active = false;

        this.offsets = [
            2.36,0.72
        ];

    },

    // start () {
    //
    // },
    startDistance : function (type,b) {
        this.type = type;


        this.tagDistance = b;
        var p = this.Hero.getPosition();
        if (!b) {
            this.updateNum = 0;
            // CustomLog("记录位置 结束 X",p.x);
            setTimeout(function () {
                this.layoutcount.active = false;
                if (this.callBack!=null) {
                    var Label = this.count.getComponent("cc.Label");
                    this.callBack(Label.string);
                }
            }.bind(this),3000);

        }
        else{
            // CustomLog("记录位置",p.x);
            this.defaultX = p.x;
            this.layoutcount.active = true;
        }
    },

    callBack : function () {

    },
    /**
     * 谁想知道结果 就谁调用这个方法 获取距离的方法
     * @param callBack
     */
    setCallBack : function (callBack) {
        this.callBack = callBack;
    },




    update : function (dt) {
        if (this.tagDistance) {
            var p = this.Hero.getPosition();
            // CustomLog("记录位置",p.x);
            if (this.defaultX != null) {
                this.calculationX = p.x - this.defaultX;
                var showX = this.calculationX/100/this.offsets[this.type];
                var Label = this.count.getComponent("cc.Label");
                Label.string = showX.toFixed(2);
                CustomLog("偏移量 X",showX);
            }
            if (this.updateNum == 0) {
                // CustomLog("记录位置 开始 X",p.x);
            }
            this.updateNum++;
        }
    }
});
