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