// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        avatars: {
            default: [],
            type: cc.SpriteFrame,
            serializable: true,
        },
        avatar1: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },

        avatar2: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },

        avatar3: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },

        Num: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        let self = this;
        cc.loader.loadResDir("Avatar", cc.SpriteFrame, (err, assets, urls) => {
            self.avatars = assets;
            // console.log("this.avatars:",self.avatars);
        })

        this.schedule(this.randomAvatar, 10);
        this.schedule(function(){
            let randomIndex = Math.ceil(Math.random() * 20)+20;
            this.Num.string = randomIndex+3 + '人';
        }.bind(this), 15);

    },
    randomAvatar() {
      
        let randomIndex = Math.ceil(Math.random() * this.avatars.length);

        let randomIndex2 = randomIndex+1>this.avatars.length-1?0:randomIndex+1;
        let randomIndex3 = randomIndex+2>this.avatars.length-1?1:randomIndex+2;

        this.avatar1.spriteFrame = this.avatars[randomIndex];
        // console.log(randomIndex,"=",randomIndex2,"=",randomIndex3);

        setTimeout(() => {
            this.avatar2.spriteFrame = this.avatars[randomIndex2];

        }, 100)

        setTimeout(() => {
            this.avatar3.spriteFrame = this.avatars[randomIndex3];

        }, 400)

    }

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