【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) {}
}

2.不挂在节点上的:一般为数据类


class GameDataMgr {
    private static _instance: GameDataMgr;

    static instance(): GameDataMgr {
        if (!this._instance) this._instance = new GameDataMgr();
        return this._instance;
    }

    test() {
        cc.log("调用成功");
    }
}
export default GuessDataMgr.instance();

使用:
1.打出类名,导入相应类
在这里插入图片描述
在这里插入图片描述![
选择想要调用的方法调用

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 待完成,注释高亮展示

忽略typescript的报错

放在要忽略的行前

单行忽略
// @ts-ignore

忽略全文
// @ts-nocheck

取消忽略全文
// @ts-check

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值