

cc.Class({
    extends: cc.Component,

    properties: {
        
        nickLabel: cc.Label,

        runMan: cc.Node,

        direction: {
            get: function () {
                if (!this._direction) {
                    this._direction = 1;
                }
                return this._direction;
            },
            set: function  (value) {
                this._direction = value;
            }
        }

    },

    init(artistData) {
        this.artistData = artistData;
        this.nickLabel.string = artistData.nick;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        // let direction = (Math.random() - 0.5) * 2;  //初始方向{1: 向左; -1: 向右}
    
        this.node.x = Math.random() * 350 * this.direction;
        this.node.y = 20;
        let moveLeftToRight = cc.moveTo(5, cc.v2(280, this.node.y));
        let moveRightToLeft = cc.moveTo(5, cc.v2(-280, this.node.y));

        let directionRightToLeftCallback = cc.callFunc(() => {
            this.runMan.runAction(cc.scaleTo(0, -1, 1));
        });

        let directionLeftToRightCallback = cc.callFunc(() => {
            this.runMan.runAction(cc.scaleTo(0, 1, 1));
        });

        if(this.direction < 0) {
            this.runMan.scaleX = -1;
            
            let ac = cc.repeatForever(
                cc.sequence([
                    moveLeftToRight,
                    directionLeftToRightCallback,
                    moveRightToLeft,
                    directionRightToLeftCallback,
                ])
            )
            this.node.runAction(ac);
        } else {
            let ac = cc.repeatForever(
                cc.sequence([
                    moveRightToLeft,
                    directionRightToLeftCallback,
                    moveLeftToRight,
                    directionLeftToRightCallback
                ])
            )
            this.node.runAction(ac);
        }

    },

    // update (dt) {},
});
