【cocos creator】TS基础
声明:const { ccclass, property } = cc._decorator;// 从 cc._decorator 命名空间中引入 ccclass 和 property 两个装饰器@ccclass // 使用装饰器声明 CCClassexport default class NewClass extends cc.Component {// ES6 Class 声明语法,继承 cc
·
声明:
const { ccclass, property } = cc._decorator;
// 从 cc._decorator 命名空间中引入 ccclass 和 property 两个装饰器
@ccclass // 使用装饰器声明 CCClass
export default class NewClass extends cc.Component {
// ES6 Class 声明语法,继承 cc.Component
@property(cc.Label)
// 使用 property 装饰器声明属性,括号里是属性类型,
//装饰器里的类型声明主要用于编辑器展示
//不加修饰默认不显示
label: cc.Label = null;
// 这里是 TypeScript 用来声明变量类型的写法,
//冒号后面是属性类型,等号后面是默认值
// 也可以使用完整属性定义格式
@property({
type: Number,
visible: true,//编辑器是否可见
displayName: '数字',//编辑器展示的文字
tooltip: '1-10',//鼠标移到displayName处,编辑器显示的提示
multiline: true,//是否换行
readonly: false,//是否只读
min: 1,
max: 10
})
number: Number = 1;
@property({
type: String,
visible: true,//编辑器是否可见
displayName: '字符串',//编辑器展示的文字
tooltip: '输入字符串',//鼠标移到displayName处,编辑器显示的提示
multiline: false,//是否换行,字符串有效
readonly: false,//是否只读
})
text: String = "";
//声明数组
@property([cc.Node])
public myNodes: cc.Node[] = [];
@property({ type: [cc.Color] })
public myColors: cc.Color[] = [];
//声明 getset
@property//下划线开头,默认不显示
_width = 100;
@property//get,set加@property则显示
get width() {
return this._width;
}
set width(value) {
cc.log('width changed');
this._width = value;
}
//声明值类型
//int
@property({ type: cc.Integer })
myInteger = 1;
@property
myNumber = 0;
//string
@property
myText = "";
//node
@property(cc.Node)
myNode: cc.Node = null;
//v2
@property
myOffset = new cc.Vec2(100, 100);
// 成员方法
onLoad() {
// init logic
this.log(1);
//this.log("xxx");//会报错,在方法中定义了参数类型,只能传指定类型的参数
this.log2("xxx");
}
log(number: Number) {//在方法中定义了参数类型,只能传指定类型的参数
console.log(number);
}
log2(text: any) {//any=>可以传任意类型
console.log(text);
}
}
这里是一个专注于游戏开发的社区,我们致力于为广大游戏爱好者提供一个良好的学习和交流平台。我们的专区包含了各大流行引擎的技术博文,涵盖了从入门到进阶的各个阶段,无论你是初学者还是资深开发者,都能在这里找到适合自己的内容。除此之外,我们还会不定期举办游戏开发相关的活动,让大家更好地交流互动。加入我们,一起探索游戏开发的奥秘吧!
更多推荐
已为社区贡献9条内容
所有评论(0)