cocos2d与cocos2d-X中的draw和update

博客介绍了cocos2d和cocos2d-X中draw和update的区别与使用。draw方法用于每帧的描绘,而update用于状态更新。建议使用scheduleUpdate或scheduleUpdateWithPriority进行更新,它们执行效率更高,内存占用少。update方法的注册和取消注册也进行了详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

像其它的游戏引擎一样,我们有两个不同的方法来完成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:)];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值