了解CCMoveToCCCallFuncNCCSequence用法-创新互联

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

成都创新互联服务项目包括凤泉网站建设、凤泉网站制作、凤泉网页制作以及凤泉网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,凤泉网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到凤泉省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

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

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

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函数看出他们参数问题

// // CallFunc // #pragma mark CCCallFunc  @implementation CCCallFunc  @synthesize targetCallback = targetCallback_;  +(id) actionWithTarget: (id) t selector:(SEL) s { 	return [[[self alloc] initWithTarget: t selector: s] autorelease]; }  -(id) initWithTarget: (id) t selector:(SEL) s { 	if( (self=[super init]) ) { 		self.targetCallback = t; 		selector_ = s; 	} 	return self; }  -(NSString*) description { 	return [NSString stringWithFormat:@"<%@ = %p | Tag = %ld | selector = %@>", 			[self class], 			self, 			(long)tag_, 			NSStringFromSelector(selector_) 			]; }  -(void) dealloc { 	[targetCallback_ release]; 	[super dealloc]; }  -(id) copyWithZone: (NSZone*) zone { 	CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_]; 	return copy; }  -(void) update:(ccTime)time { 	[self execute]; }  -(void) execute { 	[targetCallback_ performSelector:selector_]; } @end
 // // CallFuncN // #pragma mark CCCallFuncN  @implementation CCCallFuncN  -(void) execute { 	[targetCallback_ performSelector:selector_ withObject:target_]; } @end
 // // CallFuncND // #pragma mark CCCallFuncND  @implementation CCCallFuncND  @synthesize callbackMethod = callbackMethod_;  +(id) actionWithTarget:(id)t selector:(SEL)s data:(void*)d { 	return [[[self alloc] initWithTarget:t selector:s data:d] autorelease]; }  -(id) initWithTarget:(id)t selector:(SEL)s data:(void*)d { 	if( (self=[super initWithTarget:t selector:s]) ) { 		data_ = d;  #if COCOS2D_DEBUG 		NSMethodSignature * sig = [t methodSignatureForSelector:s]; // added 		NSAssert(sig !=0 , @"Signature not found for selector - does it have the following form? -(void)name:(id)sender data:(void*)data"); #endif 		callbackMethod_ = (CC_CALLBACK_ND) [t methodForSelector:s]; 	} 	return self; }  -(id) copyWithZone: (NSZone*) zone { 	CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ data:data_]; 	return copy; }  -(void) dealloc { 	// nothing to dealloc really. Everything is dealloc on super (CCCallFuncN) 	[super dealloc]; }  -(void) execute { 	callbackMethod_(targetCallback_,selector_,target_, data_); } @end
 @implementation CCCallFuncO @synthesize  object = object_;  +(id) actionWithTarget: (id) t selector:(SEL) s object:(id)object { 	return [[[self alloc] initWithTarget:t selector:s object:object] autorelease]; }  -(id) initWithTarget:(id) t selector:(SEL) s object:(id)object { 	if( (self=[super initWithTarget:t selector:s] ) ) 		self.object = object;  	return self; }  - (void) dealloc { 	[object_ release]; 	[super dealloc]; }  -(id) copyWithZone: (NSZone*) zone { 	CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ object:object_]; 	return copy; }   -(void) execute { 	[targetCallback_ performSelector:selector_ withObject:object_]; }  @end

3.CCSequence

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

id action1 = [CCMoveTo actionWithDuration:2 position:ccp(100,100)]; id action2 = [CCMoveBy actionWithDuration:2  position: ccp(80,80)]; id action3 = [CCMoveBy actionWithDuration:2  position: ccp(0,80)]; [sprite runAction: [CCSequence actions:action1, action2, action3, nil]];

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

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

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

CCSpawn

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

id action = [CCSpawn actions: 		[CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4], 		[CCRotateBy actionWithDuration: 2 angle: 720], 		nil];   [sprite runAction:action];

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

CCRepeat

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

id a1 = [CCMoveBy actionWithDuration:1 position:ccp(150,0)]; id action1 = [CCRepeat actionWithAction: 		[CCSequence actions: [CCPlace actionWithPosition:ccp(60,60)], a1, nil] 		times:3]; [sprite runAction:action1];

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

CCRepeatForever

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

CCRotateBy* rotate = [CCRotateBy actionWithDuration:1.0f angle:360]; CCRepeatForever* action2 = [CCRepeatForever actionWithAction:rotate]; [sprite runAction:action2];

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站标题:了解CCMoveToCCCallFuncNCCSequence用法-创新互联
网页URL:https://www.cdcxhl.com/article36/ceessg.html

成都网站建设公司_创新互联,为您提供网站营销手机网站建设网站收录小程序开发云服务器自适应网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设