cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // use this for initialization
    onLoad: function () {
        cocosAnalytics.CAAccount.loginStart();
        console.log('登录游戏')
        cocosAnalytics.CAAccount.loginSuccess({
            'userID': '101',
            'age': '24',    // 年龄
            'sex': '1',     // 性别：1为男，2为女，其它表示未知
        });
        this.registerEvent();
    },

    registerEvent() {
        //android HOME键
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    },

    onKeyDown(event) {
        switch (event.keyCode) {
            //注意Creator版本为2.x的请把cc.KEY.back。修改成cc.macro.KEY.back
            case cc.macro.KEY.back:
               
                if (this.isBackGame) {
                    cocosAnalytics.CAAccount.logout();
                    console.log('按了back键，退出了游戏')
                    cc.director.end();
                    return;
                }
                this.isBackGame = true;
                Toast.Show_My_Text("Press again to exit the game!");
                this.node.runAction(cc.sequence(cc.delayTime(3), cc.callFunc(() => {
                    this.isBackGame = false;
                    //this.label.string = '';
                })));
                break;
                break;
            case cc.macro.KEY.menu:
                //cocosAnalytics.CAAccount.logout();
                console.log('按了menu')
                break;
            case cc.macro.KEY.home:
                //cocosAnalytics.CAAccount.logout();
                console.log('按了Home')
                break;
        }
    },

    offEvent() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    },

    // called every frame
    update: function (dt) {

    },

    onDestroy() {
        this.offEvent();
        cocosAnalytics.CAAccount.logout();
        console.log('destory了，退出了游戏')
    }
});
