序列 –CCSequence
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
// 创建5个劢作
id ac0 = [CCPlaceactionWithPosition:p];
id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
id ac2 = [CCJumpTo actionWithDuration:2 position:ccp(150,50) height:30 jumps:5];
id ac3 = [CCBlink actionWithDuration:2 blinks:3];
id ac4 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
//将 5 个动作组合为一个序列,注意不要忘了用 nil 结尾。
id action=[CCSequence actions:ac0, ac1, ac2, ac3,ac4, ac0, nil];
[sprite runAction:action];
Spawn 的使用非常简单,该类也从 IntervalAction 派生,本身就可以被
CocosNode 对象执行。该类的作用就是同时并列执行若干个动作,但要求动作都必须是可以同时执行的。比如:移动式翻转、变色、变大小等。
需要特别注意的是,同步执行最后的完成时间由基本动作中用时最大者决定。
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite.rotation =0;
[spritesetPosition:p];
// 创建 4 个需要并行的动作,确保动作用时可组合。2 – 2 - (1+1)
id ac1 = [CCMoveToactionWithDuration:2position:ccp(s.width -50, s.height - 50)];
id ac2 = [CCRotateToactionWithDuration:2angle:180];
id ac3 = [CCScaleToactionWithDuration:1scale:4];
id ac4 = [CCScaleByactionWithDuration:1scale:0.5];
id seq = [CCSequenceactions:ac3, ac4,nil];
/ /同步动作和组合动作以形成一个连续的新动作。
id action=[CCSpawn actions:ac1, ac2, seq,nil];
[sprite runAction:action];
重复有线次数 – Repeate
重复有限的次数的动作,该类也从 IntervalAction 派生,可以被 CocosNode 对
象执行。示例代码如下:
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite .rotation = 0 ;[spritesetPosition:p];
// 创建动作序列
id ac1 = [CCMoveToactionWithDuration:2position:ccp(s.width -50, s.height - 50)];
id ac2 = [CCJumpByactionWithDuration:2position:ccp(-400, -200)height:30jumps:5];
id ac3 = [CCJumpByactionWithDuration:2position:ccp(s.width/2,0)height:20jumps:3];
id seq = [CCSequenceactions:ac1, ac2, ac3,nil];
// 重复运行上述动作序列 3次。
id action= [CCRepeatactionWithAction:seqtimes:3];
[sprite runAction:action];
反动作就是反向(逆向)执行某个动作,支持针对动作序列的反动作序列。反动作
不是一个专门的类,而是 CCFiniteAction 引入的一个接口。不是所有的类都支持
反动作,XxxxTo 类通常不支持反动作,XxxxBy 类通常支持。示例如下:
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint p = ccp(s.width/2, 50);
sprite.rotation = 0;
[sprite setPosition:p];
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(190,220)];
// 创建某个动作的反动作。
id ac2 = [ac1 reverse];
id action= [CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2];
[sprite runAction:action];
CGSize s = [[CCDirectorsharedDirector]winSize];
CGPoint p = ccp(s.width/2,50);
sprite.rotation =0;
[spritesetPosition:p];
//加载帧动画图
CCTexture2D *texture = [[CCTextureCachesharedTextureCache]addImage:@"fps_images.png"];
//初始化动画帧数组
NSMutableArray *animFrames = [NSMutableArrayarray];
for (int i=0; i<12; i++) {
CCSpriteFrame *frame =[CCSpriteFrame frameWithTexture:texture rect:CGRectMake(16*i,0,16,32)];
[animFramesaddObject:frame];
}
//每帧切换间隔0.2秒
CCAnimation *animation=[CCAnimation animationWithFrames:animFrames delay:0.2f];
//创建帧动作
id action = [CCAnimate actionWithAnimation: animation];
id actionRep=[CCRepeat actionWithAction:actiontimes:10];
[spriterunAction:actionRep];
CGSize s = [[CCDirectorsharedDirector]winSize];
CGPoint p = ccp(s.width/2,50);
sprite.rotation =0;
[spritesetPosition:p];
//加载帧动画图
CCTexture2D *texture = [[CCTextureCachesharedTextureCache]addImage:@"fps_images.png"];
//初始化动画帧数组
NSMutableArray *animFrames = [NSMutableArrayarray];
for (int i=0; i<12; i++) {
CCSpriteFrame *frame =[CCSpriteFrame frameWithTexture:texture rect:CGRectMake(16*i,0,16,32)];
[animFramesaddObject:frame];
}
//每帧切换间隔0.2秒
CCAnimation *animation=[CCAnimation animationWithFrames:animFrames delay:0.2f];
//创建帧动作
id action = [CCAnimate actionWithAnimation: animation];
//无限执行动作
id actionRep=[CCRepeatForever actionWithAction:action];
[spriterunAction:actionRep];