美文网首页
TypeScript 02——成员定义与引用

TypeScript 02——成员定义与引用

作者: GameObjectLgy | 来源:发表于2021-10-05 15:36 被阅读0次
  • 1、Cocos Creator里的Monobehavior
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({
        visible: false
    })
    text: string = 'hello';

@property({
      type: cc.Integer
  })
  myInteger = 1;

  @property
  myNumber = 0;

  @property
  myText = "";

  @property(cc.Node)
  myNode: cc.Node = null;

  @property
  myOffset = new cc.Vec2(100, 100);

    // 成员方法
    onLoad() {
        // init logic
    }
}
  • 2、 引用其他组件
    import {MyModule} from './MyModule';
    使用方法:
    @property(MyModule)
    public myModule: MyModule = null;
    然后在面板上进行赋值即可

相关文章

网友评论

      本文标题:TypeScript 02——成员定义与引用

      本文链接:https://www.haomeiwen.com/subject/wcygyltx.html