【TS技巧】【cocos creator】单例,访问代码,export,枚举

1.单例,其他类中调用单例类的方法
【TS技巧】【cocos creator】单例,访问代码,export,枚举_第1张图片

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.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

const { ccclass, property } = cc._decorator;

@ccclass
export default class testPanel extends cc.Component {

    static instance: testPanel;

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        testPanel.instance = this;
    }

    start() {
    }

    test() {
        cc.log("调用成功");
    }
    // update (dt) {}
}

使用:
1.打出类名,导入相应类
【TS技巧】【cocos creator】单例,访问代码,export,枚举_第2张图片
【TS技巧】【cocos creator】单例,访问代码,export,枚举_第3张图片【TS技巧】【cocos creator】单例,访问代码,export,枚举_第4张图片
选择想要调用的方法调用

2.数值在编辑器内展示

    @property({ type: Boolean })
    channel: boolean = false;
    @property({ type: Boolean, displayName: "测试服?", visible() { return this.channel} })
    isTestServer: boolean = true;
    //type:类型,displayName:显示的名称,visible:可见性(这里根据this.channel的值决定是否显示)

3.枚举


/**
 * 渠道id
 */
export enum Channel {
    "wei_xin" = 1,       // 微信
    "tou_tiao" = 2,      // 头条
    "vivo" = 3,      // vivo
    "oppo" = 4,      // oppo
    "qq" = 5,        // qq
}

使用:

在这里插入图片描述
3. TODO (待完成,注释高亮展示
在这里插入图片描述

        // 普通注释
        // todo 待完成,注释高亮展示
        // TODO 待完成,注释高亮展示

你可能感兴趣的:(cocos)