Cocos Creator 生命周期回调(官方文档摘录)

Cocos Creator 为组件脚本提供了生命周期的回调函数。用户通过定义特定的函数回调在特定的时期编写相关 脚本。目前提供给用户的声明周期回调函数有:

  • onLoad
  • start
  • update
  • lateUpdate
  • onDestroy
  • onEnable
  • onDisable

onLoad

组件脚本的初始化阶段,我们提供了 onLoad 回调函数。onLoad 回调会在这个组件所在的场景被载入 的时候触发,在 onLoad 阶段,保证了你可以获取到场景中的其他节点,以及节点关联的资源数据。通常 我们会在 onLoad阶段去做一些初始化相关的操作。例如:

  1.  
    cc.Class({
  2.  
    extends: cc.Component,
  3.  
     
  4.  
    properties: {
  5.  
    bulletSprite: cc.SpriteFrame,
  6.  
    gun: cc.Node,
  7.  
    },
  8.  
     
  9.  
    onLoad: function () {
  10.  
    this._bulletRect = this.bulletSprite.getRect();
  11.  
    this.gun = cc.find('hand/weapon', this.node);
  12.  
    },
  13.  
    });

start

start 回调函数会在组件第一次激活前,也就是第一次执行 update 之前触发。start 通常用于 初始化一些中间状态的数据,这些数据可能在 update 时会发生改变,并且被频繁的 enable 和 disable。

  1.  
    cc.Class({
  2.  
    extends: cc.Component,
  3.  
     
  4.  
    start: function () {
  5.  
    this._timer = 0.0;
  6.  
    },
  7.  
     
  8.  
    update: function (dt) {
  9.  
    this._timer += dt;
  10.  
    if ( this._timer >= 10.0 ) {
  11.  
    console.log('I am done!');
  12.  
    this.enabled = false;
  13.  
    }
  14.  
    },
  15.  
    });

update

游戏开发的一个关键点是在每一帧渲染前更新物体的行为,状态和方位。这些更新操作通常都放在 update 回调中。

  1.  
    cc.Class({
  2.  
    extends: cc.Component,
  3.  
     
  4.  
    update: function (dt) {
  5.  
    this.node.setPosition( 0.0, 40.0 * dt );
  6.  
    }
  7.  
    });

lateUpdate

update 会在所有动画更新前执行,但如果我们要在动画更新之后才进行一些额外操作,或者希望在所有组件的 update 都执行完之后才进行其它操作,那就需要用到 lateUpdate 回调。

  1.  
    cc.Class({
  2.  
    extends: cc.Component,
  3.  
     
  4.  
    lateUpdate: function (dt) {
  5.  
    this.node.rotation = 20;
  6.  
    }
  7.  
    });

onEnable

当组件的 enabled 属性从 false 变为 true 时,会激活 onEnable 回调。倘若节点第一次被 创建且 enabled 为 true,则会在 onLoad 之后,start 之前被调用。

onDisable

当组件的 enabled 属性从 true 变为 false 时,会激活 onDisable 回调。

onDestroy

当组件调用了 destroy(),会在该帧结束被统一回收,此时会调用 onDestroy 回调。

原文地址:https://www.cnblogs.com/luorende/p/9180706.html