• _decorator 模块
    • 索引
      • 方法
  • Details
    • 方法
      • ccclass
        • 参数列表
      • 示例
      • property
        • 参数列表
      • 示例
      • executeInEditMode
      • 示例
      • requireComponent
        • 参数列表
      • 示例
      • menu
        • 参数列表
      • 示例
      • executionOrder
        • 参数列表
      • 示例
      • disallowMultiple
      • 示例
      • playOnFocus
      • 示例
      • inspector
        • 参数列表
      • 示例
      • icon
        • 参数列表
      • 示例
      • help
        • 参数列表
      • 示例
      • mixins
        • 参数列表
      • 示例

    _decorator 模块

    一些 JavaScript 装饰器,目前可以通过 "cc._decorator" 来访问。(这些 API 仍不完全稳定,有可能随着 JavaScript 装饰器的标准实现而调整)

    索引

    方法
    • ccclass 将标准写法的 ES6 Class 声明为 CCClass,具体用法请参阅类型定义。
    • property 定义 CCClass 所用的属性。
    • executeInEditMode 允许继承自 Component 的 CCClass 在编辑器里执行。
    • requireComponent 为声明为 CCClass 的组件添加依赖的其它组件。
    • menu 将当前组件添加到组件菜单中,方便用户查找。
    • executionOrder 设置脚本生命周期方法调用的优先级。
    • disallowMultiple 防止多个相同类型(或子类型)的组件被添加到同一个节点。
    • playOnFocus 当指定了 "executeInEditMode" 以后,playOnFocus 可以在选中当前组件所在的节点时,提高编辑器的场景刷新频率到 60 FPS,否则场景就只会在必要的时候进行重绘。
    • inspector 自定义当前组件在 属性检查器 中渲染时所用的网页 url。
    • icon 自定义当前组件在编辑器中显示的图标 url。
    • help 指定当前组件的帮助文档的 url,设置过后,在 属性检查器 中就会出现一个帮助图标,用户点击将打开指定的网页。
    • mixins NOTE:…

    Details

    方法

    ccclass

    将标准写法的 ES6 Class 声明为 CCClass,具体用法请参阅类型定义。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:243
    参数列表
    • name String The class name used for serialization.
    示例
    1. const {ccclass} = cc._decorator;
    2. // define a CCClass, omit the name
    3. @ccclass
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    7. // define a CCClass with a name
    8. @ccclass('LoginData')
    9. class LoginData {
    10. // ...
    11. }
    property

    定义 CCClass 所用的属性。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:315
    参数列表
    • options Object an object with some property attributes
      • type Any
      • visible Boolean | Function
      • displayName String
      • tooltip String
      • multiline Boolean
      • readonly Boolean
      • min Number
      • max Number
      • step Number
      • range Number[]
      • slide Boolean
      • serializable Boolean
      • editorOnly Boolean
      • override Boolean
      • animatable Boolean
      • formerlySerializedAs String
    示例
    1. const {ccclass, property} = cc._decorator;
    2. @ccclass
    3. class NewScript extends cc.Component {
    4. @property({
    5. type: cc.Node
    6. })
    7. targetNode1 = null;
    8. @property(cc.Node)
    9. targetNode2 = null;
    10. @property(cc.Button)
    11. targetButton = null;
    12. @property
    13. _width = 100;
    14. @property
    15. get width () {
    16. return this._width;
    17. }
    18. @property
    19. set width (value) {
    20. this._width = value;
    21. }
    22. @property
    23. offset = new cc.Vec2(100, 100);
    24. @property(cc.Vec2)
    25. offsets = [];
    26. @property(cc.SpriteFrame)
    27. frame = null;
    28. }
    29. // above is equivalent to (上面的代码相当于):
    30. var NewScript = cc.Class({
    31. properties: {
    32. targetNode1: {
    33. default: null,
    34. type: cc.Node
    35. },
    36. targetNode2: {
    37. default: null,
    38. type: cc.Node
    39. },
    40. targetButton: {
    41. default: null,
    42. type: cc.Button
    43. },
    44. _width: 100,
    45. width: {
    46. get () {
    47. return this._width;
    48. },
    49. set (value) {
    50. this._width = value;
    51. }
    52. },
    53. offset: new cc.Vec2(100, 100)
    54. offsets: {
    55. default: [],
    56. type: cc.Vec2
    57. }
    58. frame: {
    59. default: null,
    60. type: cc.SpriteFrame
    61. },
    62. }
    63. });
    executeInEditMode

    允许继承自 Component 的 CCClass 在编辑器里执行。默认情况下,所有 Component 都只会在运行时才会执行,也就是说它们的生命周期回调不会在编辑器里触发。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:461
    示例
    1. const {ccclass, executeInEditMode} = cc._decorator;
    2. @ccclass
    3. @executeInEditMode
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    requireComponent

    为声明为 CCClass 的组件添加依赖的其它组件。当组件添加到节点上时,如果依赖的组件不存在,引擎将会自动将依赖组件添加到同一个节点,防止脚本出错。该设置在运行时同样有效。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:485
    参数列表
    • requiredComponent Component
    示例
    1. const {ccclass, requireComponent} = cc._decorator;
    2. @ccclass
    3. @requireComponent(cc.Sprite)
    4. class SpriteCtrl extends cc.Component {
    5. // ...
    6. }
    menu

    将当前组件添加到组件菜单中,方便用户查找。例如 "Rendering/CameraCtrl"。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:506
    参数列表
    • path String The path is the menu represented like a pathname.
    1. For example the menu could be "Rendering/CameraCtrl".
    示例
    1. const {ccclass, menu} = cc._decorator;
    2. @ccclass
    3. @menu("Rendering/CameraCtrl")
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    executionOrder

    设置脚本生命周期方法调用的优先级。优先级小于 0 的组件将会优先执行,优先级大于 0 的组件将会延后执行。优先级仅会影响 onLoad, onEnable, start, update 和 lateUpdate,而 onDisable 和 onDestroy 不受影响。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:528
    参数列表
    • order Number The execution order of lifecycle methods for Component. Those less than 0 will execute before while those greater than 0 will execute after.
    示例
    1. const {ccclass, executionOrder} = cc._decorator;
    2. @ccclass
    3. @executionOrder(1)
    4. class CameraCtrl extends cc.Component {
    5. // ...
    6. }
    disallowMultiple

    防止多个相同类型(或子类型)的组件被添加到同一个节点。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:551
    示例
    1. const {ccclass, disallowMultiple} = cc._decorator;
    2. @ccclass
    3. @disallowMultiple
    4. class CameraCtrl extends cc.Component {
    5. // ...
    6. }
    playOnFocus

    当指定了 "executeInEditMode" 以后,playOnFocus 可以在选中当前组件所在的节点时,提高编辑器的场景刷新频率到 60 FPS,否则场景就只会在必要的时候进行重绘。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:572
    示例
    1. const {ccclass, playOnFocus, executeInEditMode} = cc._decorator;
    2. @ccclass
    3. @executeInEditMode
    4. @playOnFocus
    5. class CameraCtrl extends cc.Component {
    6. // ...
    7. }
    inspector

    自定义当前组件在 属性检查器 中渲染时所用的网页 url。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:595
    参数列表
    • url String
    示例
    1. const {ccclass, inspector} = cc._decorator;
    2. @ccclass
    3. @inspector("packages://inspector/inspectors/comps/camera-ctrl.js")
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    icon

    自定义当前组件在编辑器中显示的图标 url。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:616
    参数列表
    • url String
    示例
    1. const {ccclass, icon} = cc._decorator;
    2. @ccclass
    3. @icon("xxxx.png")
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    help

    指定当前组件的帮助文档的 url,设置过后,在 属性检查器 中就会出现一个帮助图标,用户点击将打开指定的网页。

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:638
    参数列表
    • url String
    示例
    1. const {ccclass, help} = cc._decorator;
    2. @ccclass
    3. @help("app://docs/html/components/spine.html")
    4. class NewScript extends cc.Component {
    5. // ...
    6. }
    mixins

    NOTE:The old mixins implemented in cc.Class(ES5) behaves exact the same as multiple inheritance.But since ES6, class constructor can't be function-called and class methods become non-enumerable,so we can not mix in ES6 Classes.See:https://esdiscuss.org/topic/traits-are-now-impossible-in-es6-until-es7-since-rev32One possible solution (but IDE unfriendly):http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classesNOTE:You must manually call mixins constructor, this is different from cc.Class(ES5).

    metadescription
    定义于cocos2d/core/platform/CCClassDecorator.js:661
    参数列表
    • ctor Function constructors to mix, only support ES5 constructors or classes defined by using cc.Class,
    1. not support ES6 Classes.
    示例
    1. const {ccclass, mixins} = cc._decorator;
    2. class Animal { ... }
    3. const Fly = cc.Class({
    4. constructor () { ... }
    5. });
    6. @ccclass
    7. @mixins(cc.EventTarget, Fly)
    8. class Bird extends Animal {
    9. constructor () {
    10. super();
    11. // You must manually call mixins constructor, this is different from cc.Class(ES5)
    12. cc.EventTarget.call(this);
    13. Fly.call(this);
    14. }
    15. // ...
    16. }