cocos2d与cocos2d-X中的draw和update

像其它的游戏引擎一样,我们有两个不同的方法来完成drawupdate

        1: Draw:每一个CCNode都有一个draw方法,每一帧都会调用。我们只在这个方法里做描绘的事情。

        2: Update:默认情况下,CCNode是没有update的,如果你想要更新状态,你可以注册一个回调函数,有以下四种方式

           scheduleUpdate:

           scheduleUpdateWithPriority:

           scheduleSelector:(SEL)selector

           scheduleSelector: (SEL)selector interval:(float)interval

         注册的方法只有当前是活动的场景,并且没有Pause的情况下才会调。我们推荐使用scheduleUpdate或者scheduleUpdateWithPriority。因为它运行更快,占用内存更少。例如:

-(id) init
{
  // -10代表优先级,越小则优先级越高,也就越先被执行
   [self scheduleUpdateWithPriority:-10];
}
-(void) update:(ccTime)deltaTime
{
   // update your node here
   // DON'T draw it, JUST update it. 
   // example:
   rotation_ = value * deltaTime;
}
 

调用了scheduleUpdate或者scheduleUpdateWithPriority后,系统就会去查找我们定义的update方法,相当于schedule了update方法。去除注册的update方法可以使用:

[node unscheduleUpdate];

        我们也可以使用scheduleSelector,这是一种比较老的方式,没有优先级。如果没有interval或者当interval==0的时表示每一帧都调用。

-(id) init
{
   // the "step:" selector will be called every 0.5 seconds
   [self schedule:@selector(step:) interval:0.5f]
}
 
-(void) step:(ccTime) deltaTime
{
   // update your stuff here
}

注销掉该方法:

  [node unscheduleSelector:@selector(step:)];
原文地址:https://www.cnblogs.com/javawebsoa/p/3230773.html