Cocoa学习笔记 Cocos2d 各种动作介绍 (下)

移动开发 iOS 游戏开发
本文介绍的是Cocos学习笔记 Cocos2d 各种动作介绍,主要讲述了几个常用的动作。我们先来看内容。

Cocoa学习笔记 Cocos2d 各种动作介绍 (上)是本节介绍的内容,继续Cocoa学习笔记 Cocos2d 各种动作介绍 (上)的内容开始介绍。我们先来看内容。

重复有限次数 – Repeate

重复有限的次数的动作示例代码如下:

  1.  (void) OnRepeat:(id) sender {    
  2.  CGSize s = [[CCDirector sharedDirector] winSize];     
  3.  CGPoint p = ccp(s.width/2, 50);    
  4.  sprite.rotation = 0;     
  5.  [sprite setPosition:p];    
  6.  // 创建动作序列    
  7.  id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];    
  8. id ac2 = [CCJumpBy actionWithDuration:2 position:ccp(-400, -200) height:30 jumps:5];    
  9. id ac3 = [CCJumpBy actionWithDuration:2 position:ccp(s.width/2, 0) height:20 jumps:3];    
  10.  id seq = [CCSequence actions:ac1, ac2, ac3, nil];    
  11.  //重复运行上述劢作序列 3 次。    
  12.  [sprite runAction:[CCRepeat actionWithAction:seq times:3]];    
  13. }  

反动作 – Reverse

反动作就是反向(逆向)执行某个动作,支持针对动作序列的反动作序列。反动作不是一个专门的类,而是 CCFiniteAction 引入的一个接口。不是所有的类都支持反动作,XxxxTo 类通常不支持反动作,XxxxBy 类通常支持。示例如下:

  1. (void) OnReverse:(id) sender {    
  2.  CGSize s = [[CCDirector sharedDirector] winSize];     
  3.  CGPoint p = ccp(s.width/2, 50);    
  4.  sprite.rotation = 0;     
  5.  [sprite setPosition:p];    
  6.  id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(190, 220)];    
  7. // 创建某个劢作癿反劢作。    
  8.  id ac2 = [ac1 reverse];    
  9.  [sprite runAction:[CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2]];    
  10. }   
  11.  
  12. 动画 – Animation  
  13.  
  14. 动画就是让精灵自身的连续执行一段影像,形成模拟运动的效果:行走时动精灵状态、打斗时的状态等。  
  15.  
  16.  (void) OnAnimation:(id) sender {    
  17.  
  18. CCAnimation *animation = [AtlasAnimation animationWithName:@"flight" delay:0.2f];    
  19.  // 每帧的内容定义。     
  20. for(int i=0;i<3;i++) {    
  21.  int xi % 3;     
  22.  [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];     
  23.  }    
  24. // 执行劢画效果     
  25.  id action = [CCAnimate actionWithAnimation: animation];     
  26. [sprite runAction:[CCRepeat actionWithAction:action times:10]];    
  27.  }   
  28.  
  29. 无限重复 – RepeatForever   
  30.  
  31. RepeatForever 是从 Action 类直接派生的,因此无法参于序列和同步;自身也无法反向执行。该类的作用就是无限期执行某个动作或动作序列,直到被停止。  
  32.  
  33.  (void) OnRepeatForever:(id) sender {    
  34.  CGSize s = [[Director sharedDirector] winSize];     
  35.  CGPoint p = ccp(100, 50);    
  36. // 飞行喷火模拟劢画    
  37.  CCAnimation *animation = [CCAnimation animationWithName:@"flight" delay:0.1f];    
  38.  for(int i=0;i<3;i++)     
  39.  {    
  40.  int xi % 3;    
  41.  [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];     
  42.  }    
  43.  id action = [CCAnimate actionWithAnimation: animation];    
  44.  // 将该动画作为精灵的本征动画,一直运行。    
  45.  [sprite runAction:[RepeatForever actionWithAction:action]];    
  46. // 在创建第二个连续无限期动作序列。叠加二者形成完整效果。     
  47. ccBezierConfig bezier;    
  48.  sprite.rotation = 0;     
  49.  [sprite setPosition:p];    
  50.  bezier.startPosition = ccp(0,0);     
  51.  bezier.controlPoint_1 = ccp(0, s.height/2);     
  52.  bezier.controlPoint_2 = ccp(300, -s.height/2);     
  53.  bezier.endPosition = ccp(300,100);    
  54.  id ac10 = [CCBezierBy actionWithDuration:3 bezier:bezier];    
  55.  id ac11 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];    
  56.  id ac1 = [CCSpawn actions:ac10, [Repeat actionWithAction:ac11 times:4], nil];    
  57.  id ac2 = [CCSpawn actions:[ac10 reverse], [CCRepeat actionWithAction:ac11 times:4], nil];    
  58.  // 第二个无限期连续运劢。    
  59.  [sprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];    
  60.  }  

速度变化

基本动作和组合动作实现了针对精灵的各种运动、动画效果的改变,但这样的改变的速度是不变的,通过 CCEaseAction 为基类的类系和 CCSpeed 类我们可以很方便的修改精灵执行动作的速度:由快至慢还是由慢至快

EaseIn 由慢至快。

EaseOut 由快至慢

EaseInOut 由慢至快再由快至慢。

EaseSineIn 由慢至快

EaseSineOut 由快至慢

EaseSineInOut 由慢至快再由快至慢。

EaseExponentialIn 由慢至极快。

EaseExponentialOut 由极快至慢。

EaseExponentialInOut 由慢至极快再由极快至慢。

Speed 人工设定速度,还可通过 SetSpeed 不断调整。

扩展动作

我们已经掌握了执行各种各样的动作,也可以按照不同的快慢修改动作执行的时间, Cocos2D-iPhone 还提供了针对现有动作的扩展,以实现各种灵活的效果。

延时动作 – Delay在动作序列中增加一个时间间歇:

  1. (void) OnDelay:(id) sender {    
  2.  id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];    
  3.  id ac2 = [ac1 reverse];    
  4. // 实现一个等待间歇    
  5. [spriterunAction:[Sequenceactions:ac1,[DelayTime actionWithDuration:1], ac2, nil]];     
  6. }  

函数调用 

函数在动作序列中间或者结束调用某个函数,执行任何需要执行的任务:动作、状态修改等。代码如下:

  1. (void) OnCallFunc:(id) sender {    
  2. id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];    
  3. id ac2 = [ac1 reverse];    
  4. id acf = [CCCallFunc actionWithTarget:self selector:@selector(CallBack1)];    
  5. [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];    
  6. }  

对应的函数为:(再做一个动作,这就实现了动作、动作序列的任意扩展和连接)

  1. (void) CallBack1 {    
  2.  [sprite runAction:[CCTintBy actionWithDuration:0.5 red:255 green:0 blue:255]];     
  3.  }  

带对象参数 调用自定义函数时,传递当前对象。

  1. (void) OnCallFuncN:(id) sender {    
  2.  id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];    
  3.  id ac2 = [ac1 reverse];    
  4. id acf = [CallFuncN actionWithTarget:self selector:@selector(CallBack2:)];    
  5.  [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];    
  6.  }  

对应的自定义函数:(这里,我们直接使用了该对象)

  1. (void) CallBack2:(id)sender {    
  2.  [sender runAction:[CCTintBy actionWithDuration:1 red:255 green:0 blue:255]];     
  3. }  

带对象、数据参数调用自定义函数时,传递当前对象和一个常量(也可以是指针)。

  1. (void) OnCallFuncND:(id) sender {    
  2.  id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];    
  3.  id ac2 = [ac1 reverse];    
  4.  id acf = [CCCallFuncND actionWithTarget:self selector:@selector(CallBack3:data:) data:(void*)2];    
  5.  [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];    
  6.  }  

对应的自定义函数,我们使用了传递的对象和数据:

  1. (void) CallBack3:(id)sender data:(void*)data {    
  2. [sender runAction:[CCTintBy actionWithDuration:(NSInteger)data red:255 green:0 blue:255]]; }  

小结:Cocoa学习笔记 Cocos2d 各种动作介绍 (下)的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-07-08 16:09:54

Cocoa Cocos2d 动作

2011-08-11 18:00:18

Cocos2d动作Action

2011-08-11 17:52:01

Cocos2d游戏对象

2011-08-02 15:37:48

Cocos2D UIAccelero

2011-08-09 16:08:58

IOS游戏Cocos2d

2011-08-09 16:25:16

Cocos2d视图坐标

2011-08-02 15:47:28

Cocos2D Animation

2011-07-29 18:02:06

2011-08-17 15:04:48

Cocos2DUIAccelerom加速计

2011-07-27 10:13:23

Cocos2D iPhone

2012-06-01 10:27:44

Cocos2d触摸分发原理

2011-08-11 14:32:04

iPhone游戏Cocos2dActions

2011-07-26 10:42:00

Cocoa Cocoa2d 游戏

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2011-07-27 14:48:21

iPhone Cocos2D 坐标

2011-08-08 11:26:39

Cocos2d 游戏 Class类

2011-08-08 17:17:55

Cocos2D 坐标 OpenglES

2011-07-20 14:04:46

Cocos2d iPhone 游戏

2011-07-27 13:44:08

2011-08-22 10:49:42

Cocos2d 开发CCLayerTouch事件
点赞
收藏

51CTO技术栈公众号