了解CCMoveTo CCCallFuncN CCSequence用法

本文详细介绍了在Cocos2D游戏中使用序列控制蜘蛛移动的方法及动作序列的应用,包括CCMoveTo、CCCallFuncN等动作的具体实现与功能,以及如何通过CCSequence组织多个动作以实现复杂的游戏行为。

在《iPhone & iPad Cocos2D游戏开发实战》一书中在看第四章时候遇到陌生知识,然后在网上找到相关知识点,再此记录;

由序列控制蜘蛛的移动方法代码

[cpp]  view plain copy
  1. -(void) runSpiderMoveSequence:(CCSprite*)spider {  
  2. // 随着时间慢慢增加蜘蛛的移动速度   
  3. numSpidersMoved++;//定义的int型变量  
  4. if (numSpidersMoved % 8 == 0 && spiderMoveDuration > 2.0f) {  
  5. spiderMoveDuration -= 0.1f; }  
  6. // 用于控制蜘蛛移动的动作序列  
  7. CGPoint belowScreenPosition = CGPointMake(spider.position.x,  
  8. -[spider texture].contentSize.height);  
  9. CCMoveTo* move = [CCMoveTo actionWithDuration:spiderMoveDuration position:belowScreenPosition];  
  10. CCCallFuncN* call = [CCCallFuncN actionWithTarget:self selector:@selector(spiderBelowScreen:)];  
  11. CCSequence* sequence = [CCSequence actions:move, call, nil];  
  12. [spider runAction:sequence];   
  13. }  

RunSpiderMoveSequence方法的作用是跟踪已被放下的蜘蛛数量。每次到第八个蜘蛛时,spiderMoveDuration的值就会被减少,从而提高所有蜘蛛的移动速度。%这个符号叫作“余数运算子”(Modulus Operator),用于得到运用除法以后得到的余数。比如,如果numSpidersMoved可以用8除尽,那么“余数运算子”的计算结果就应该是0。

这里用到的动作序列只有一个CCMoveTo动作和一个CCCallFuncN动作。你可以改进蜘蛛的行为,比如让它往下移动一点,等个几秒钟,然后一直移动到底部,就像真的邪恶的蜘蛛通常会做的那样。我将把具体的做法留给你去发挥。我选择CCCallFuncN的目的是给spiderBelowScreen方法传递蜘蛛精灵作为它的sender变量。这样的话,当某只蜘蛛到达屏幕底部时,我就可以直接引用那个蜘蛛,不需要再去到处找了 


1.CCMoveTo  

表示移动到某一个点,还有一个与它类似的CCMoveBy表示移动相对于当前位置某个位置,相当于一个向量;


2.CCCallFuncN

CCCallFuncN 带有一个参数,这个参数本身是一个Action,相当于他的参数就是一个BUtton;与它类似的还有

CCCallFunc 不带参数, 执行回调函数方法,

CCCallFuncND 带两个参数,一个是Action动作,另一个是自定义的参数

CCCallFuncO 也是两个参数,和CCCallFuncN参数一样,


以下是几个类在CCActionInstant.m文件中的定义,通过他们的-(void)execute函数看出他们参数问题

[cpp]  view plain copy
  1. //  
  2. // CallFunc  
  3. //  
  4. #pragma mark CCCallFunc  
  5.   
  6. @implementation CCCallFunc  
  7.   
  8. @synthesize targetCallback = targetCallback_;  
  9.   
  10. +(id) actionWithTarget: (id) t selector:(SEL) s  
  11. {  
  12.     return [[[self alloc] initWithTarget: t selector: s] autorelease];  
  13. }  
  14.   
  15. -(id) initWithTarget: (id) t selector:(SEL) s  
  16. {  
  17.     if( (self=[super init]) ) {  
  18.         self.targetCallback = t;  
  19.         selector_ = s;  
  20.     }  
  21.     return self;  
  22. }  
  23.   
  24. -(NSString*) description  
  25. {  
  26.     return [NSString stringWithFormat:@"<%@ = %p | Tag = %ld | selector = %@>",  
  27.             [self class],  
  28.             self,  
  29.             (long)tag_,  
  30.             NSStringFromSelector(selector_)  
  31.             ];  
  32. }  
  33.   
  34. -(void) dealloc  
  35. {  
  36.     [targetCallback_ release];  
  37.     [super dealloc];  
  38. }  
  39.   
  40. -(id) copyWithZone: (NSZone*) zone  
  41. {  
  42.     CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_];  
  43.     return copy;  
  44. }  
  45.   
  46. -(void) update:(ccTime)time  
  47. {  
  48.     [self execute];  
  49. }  
  50.   
  51. -(void) execute  
  52. {  
  53.     [targetCallback_ performSelector:selector_];  
  54. }  
  55. @end  
[cpp]  view plain copy
  1. //  
  2. // CallFuncN  
  3. //  
  4. #pragma mark CCCallFuncN  
  5.   
  6. @implementation CCCallFuncN  
  7.   
  8. -(void) execute  
  9. {  
  10.     [targetCallback_ performSelector:selector_ withObject:target_];  
  11. }  
  12. @end  
[cpp]  view plain copy
  1. //  
  2. // CallFuncND  
  3. //  
  4. #pragma mark CCCallFuncND  
  5.   
  6. @implementation CCCallFuncND  
  7.   
  8. @synthesize callbackMethod = callbackMethod_;  
  9.   
  10. +(id) actionWithTarget:(id)t selector:(SEL)s data:(void*)d  
  11. {  
  12.     return [[[self alloc] initWithTarget:t selector:s data:d] autorelease];  
  13. }  
  14.   
  15. -(id) initWithTarget:(id)t selector:(SEL)s data:(void*)d  
  16. {  
  17.     if( (self=[super initWithTarget:t selector:s]) ) {  
  18.         data_ = d;  
  19.   
  20. #if COCOS2D_DEBUG  
  21.         NSMethodSignature * sig = [t methodSignatureForSelector:s]; // added  
  22.         NSAssert(sig !=0 , @"Signature not found for selector - does it have the following form? -(void)name:(id)sender data:(void*)data");  
  23. #endif  
  24.         callbackMethod_ = (CC_CALLBACK_ND) [t methodForSelector:s];  
  25.     }  
  26.     return self;  
  27. }  
  28.   
  29. -(id) copyWithZone: (NSZone*) zone  
  30. {  
  31.     CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ data:data_];  
  32.     return copy;  
  33. }  
  34.   
  35. -(void) dealloc  
  36. {  
  37.     // nothing to dealloc really. Everything is dealloc on super (CCCallFuncN)  
  38.     [super dealloc];  
  39. }  
  40.   
  41. -(void) execute  
  42. {  
  43.     callbackMethod_(targetCallback_,selector_,target_, data_);  
  44. }  
  45. @end  
[cpp]  view plain copy
  1. @implementation CCCallFuncO  
  2. @synthesize  object = object_;  
  3.   
  4. +(id) actionWithTarget: (id) t selector:(SEL) s object:(id)object  
  5. {  
  6.     return [[[self alloc] initWithTarget:t selector:s object:object] autorelease];  
  7. }  
  8.   
  9. -(id) initWithTarget:(id) t selector:(SEL) s object:(id)object  
  10. {  
  11.     if( (self=[super initWithTarget:t selector:s] ) )  
  12.         self.object = object;  
  13.   
  14.     return self;  
  15. }  
  16.   
  17. - (void) dealloc  
  18. {  
  19.     [object_ release];  
  20.     [super dealloc];  
  21. }  
  22.   
  23. -(id) copyWithZone: (NSZone*) zone  
  24. {  
  25.     CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ object:object_];  
  26.     return copy;  
  27. }  
  28.   
  29.   
  30. -(void) execute  
  31. {  
  32.     [targetCallback_ performSelector:selector_ withObject:object_];  
  33. }  
  34.   
  35. @end  

3.CCSequence 

sequence是用来按顺序执行一系列的动作,即动作按排列的顺序一个接一个的执行,示例如下:

[cpp]  view plain copy
  1. id action1 = [CCMoveTo actionWithDuration:2 position:ccp(100,100)];  
  2. id action2 = [CCMoveBy actionWithDuration:2  position: ccp(80,80)];  
  3. id action3 = [CCMoveBy actionWithDuration:2  position: ccp(0,80)];  
  4. [sprite runAction: [CCSequence actions:action1, action2, action3, nil]];  

上面这段代码的意思是,sprite(精灵对象)先移动到坐标(100,100)位置,然后在向右上方移动(8080),然后,再向右移动8080,0)。这一系列动作是不重叠,一个接一个的执行的。 

注意的是,在这些动作中不能有 CCRepeatForever 这种无限的动作(就是不停的一直持续的动作),必须是那种可以在有限的时间内完成的。 


另外在博客上看到其他几个类似的类的用法,都是cocos2d常用动作 原文连接 http://leeyin.iteye.com/blog/1306557

CCSpawn 

这个与上面的 CCSequence 不同的是,排列的动作是同时执行的,执行的时间以子动作中的最长的时间为准。代码示例:

[cpp]  view plain copy
  1. id action = [CCSpawn actions:  
  2.         [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4],  
  3.         [CCRotateBy actionWithDuration: 2 angle: 720],  
  4.         nil];  
  5.    
  6. [sprite runAction:action];  

上面这段代码的意思是,sprite 在两秒钟内,向右跳四次,总共跳跃距离是300,跳跃高度是50,在跳跃过程中同时旋转720度。 


CCRepeat 

这个是用来重复一个动作有限的次数。当然,你也可以用CCSequence来实现同样的功能,只是那样看起来有点傻。示例:

[cpp]  view plain copy
  1. id a1 = [CCMoveBy actionWithDuration:1 position:ccp(150,0)];  
  2. id action1 = [CCRepeat actionWithAction:  
  3.         [CCSequence actions: [CCPlace actionWithPosition:ccp(60,60)], a1, nil]  
  4.         times:3];  
  5. [sprite runAction:action1];  

上面这段代码的意思是,先将sprite 放置在(60,60)位置,然后一秒内向右移动150的距离。这两个动作重复3次。


CCRepeatForever 

上面的是重复有限次数,这个是无限次重复,比如,你想让一个轮子不停的旋转,就可以用这个实现。示例:

[cpp]  view plain copy
  1. CCRotateBy* rotate = [CCRotateBy actionWithDuration:1.0f angle:360];  
  2. CCRepeatForever* action2 = [CCRepeatForever actionWithAction:rotate];  
  3. [sprite runAction:action2];  

就像上面讲的这段代码会让这个 sprite 一直不停的 以每秒 360 度的转速永远的旋转下去。  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值