schedule

本文详细介绍了Cocos2d-x中CCNode的schedule成员函数的各种用法,包括不同频率的任务调度、指定时间间隔的调度、重复次数有限的调度、一次性调度以及如何取消这些调度。适用于希望掌握Cocos2d-x定时任务机制的游戏开发者。

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

schedule是CCNode的成员函数,作用是任务调度。即每隔一段时间调用一次方法。在很多情况下,你需要节点调用指定的更新方法以处理某些情况,比如碰撞检测。

一、scheduleUpdate

 1 - (id)init {
 2     if (self=[super init]) {
 3         [self scheduleUpdate]; //此方法会每一帧调用一次update:方法
 4     }
 5 }
 6 
 7 // 重载CCNode的update:方法
 8 // delta参数表示此方法的最后一次调用到现在所经过的时间
 9 - (void)update:(ccTime)delta {
10     CCLOG(@"%f", delta);
11 }

二、schedule:(SEL)selector

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {    
 4         [self schedule:@selector(test:)]; //使用此方法可以指定更新方法
 5     }
 6     return self;    
 7 }
 8 
 9 - (void)test:(ccTime)delta
10 {
11     CCLOG(@"%f", delta);
12 }

三、schedule:(SEL)selector interval:(ccTime)interval

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {
 4         // 使用此方法可以指定更新方法和时间间隔
 5         [self schedule:@selector(test:) interval:10.0f];
 6     }
 7     return self;    
 8 }
 9 
10 - (void)test:(ccTime)delta
11 {
12     CCLOG(@"%f", delta);
13 }

四、schedule:(SEL)selector interval:(ccTime)interval repeat: (uint) repeat delay:(ccTime) delay

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {
 4         // 调用此方法1秒后会调用一次test:方法
 5         // 之后每隔10秒调用一次test:方法,调用3次
 6         // 总共会调用4次test:方法
 7         [self schedule:@selector(test:) interval:10.0f repeat:3 delay:1.0f];
 8     }
 9     return self;    
10 }
11 
12 - (void)test:(ccTime)delta
13 {
14     CCLOG(@"%f", delta);
15 }

五、scheduleOnce:(SEL) selector delay:(ccTime) delay

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {
 4         // 使用此方法只会调用一次更新方法,delay指定时间间隔
 5         [self scheduleOnce:@selector(test:) delay:1.0f];
 6     }
 7     return self;    
 8 }
 9 
10 - (void)test:(ccTime)delta
11 {
12     CCLOG(@"%f", delta);
13 }

六、scheduleUpdateWithPriority:(NSInteger)priority

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {
 4         // 使用此方法可以指定优先级
 5         [self scheduleUpdateWithPriority:1];
 6     }
 7     return self;    
 8 }
 9 
10 - (void)update:(ccTime)delta
11 {
12     CCLOG(@"%f", delta);
13 }

七、停止调度

1 // 停止某个指定的选择器,此方法不会停止scheduleUpdate里面设置的选择器
2 [self unschedule:@selector(test:)];
3 // 停止scheduleUpdate里面设置的选择器
4 [self unscheduleUpdate];
5 // 停止所有选择器,包括在scheduleUpdate里面设置的选择器
6 [self unscheduleAllSelectors];

八、_cmd关键词

 1 -(id) init
 2 {
 3     if( (self=[super init]) ) {    
 4         [self schedule:@selector(test:) interval:1.0f];
 5     }
 6     return self;    
 7 }
 8 
 9 - (void)test:(ccTime)delta
10 {
11     CCLOG(@"%f", delta);
12     // 使用_cmd关键词停止当前方法的调度
13     [self unschedule:_cmd];
14     // 使用_cmd关键词来代替选择器,用新的时间间隔重新调度
15     [self schedule:_cmd interval:10.0f];
16 }

 

 

转载于:https://www.cnblogs.com/lanelife/archive/2013/05/02/3054879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值