自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (下)

移動開發(fā) iOS 游戲開發(fā)
本文介紹的是Cocos學(xué)習(xí)筆記 Cocos2d 各種動作介紹,主要講述了幾個常用的動作。我們先來看內(nèi)容。

Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (上)是本節(jié)介紹的內(nèi)容,繼續(xù)Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (上)的內(nèi)容開始介紹。我們先來看內(nèi)容。

重復(fù)有限次數(shù) – Repeate

重復(fù)有限的次數(shù)的動作示例代碼如下:

  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.  // 創(chuàng)建動作序列    
  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.  //重復(fù)運行上述勱作序列 3 次。    
  12.  [sprite runAction:[CCRepeat actionWithAction:seq times:3]];    
  13. }  

反動作 – Reverse

反動作就是反向(逆向)執(zhí)行某個動作,支持針對動作序列的反動作序列。反動作不是一個專門的類,而是 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. // 創(chuàng)建某個勱作癿反勱作。    
  8.  id ac2 = [ac1 reverse];    
  9.  [sprite runAction:[CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2]];    
  10. }   
  11.  
  12. 動畫 – Animation  
  13.  
  14. 動畫就是讓精靈自身的連續(xù)執(zhí)行一段影像,形成模擬運動的效果:行走時動精靈狀態(tài)、打斗時的狀態(tài)等。  
  15.  
  16.  (void) OnAnimation:(id) sender {    
  17.  
  18. CCAnimation *animation = [AtlasAnimation animationWithName:@"flight" delay:0.2f];    
  19.  // 每幀的內(nèi)容定義。     
  20. for(int i=0;i<3;i++) {    
  21.  int xi % 3;     
  22.  [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];     
  23.  }    
  24. // 執(zhí)行勱畫效果     
  25.  id action = [CCAnimate actionWithAnimation: animation];     
  26. [sprite runAction:[CCRepeat actionWithAction:action times:10]];    
  27.  }   
  28.  
  29. 無限重復(fù) – RepeatForever   
  30.  
  31. RepeatForever 是從 Action 類直接派生的,因此無法參于序列和同步;自身也無法反向執(zhí)行。該類的作用就是無限期執(zhí)行某個動作或動作序列,直到被停止。  
  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. // 在創(chuàng)建第二個連續(xù)無限期動作序列。疊加二者形成完整效果。     
  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.  // 第二個無限期連續(xù)運勱。    
  59.  [sprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];    
  60.  }  

速度變化

基本動作和組合動作實現(xiàn)了針對精靈的各種運動、動畫效果的改變,但這樣的改變的速度是不變的,通過 CCEaseAction 為基類的類系和 CCSpeed 類我們可以很方便的修改精靈執(zhí)行動作的速度:由快至慢還是由慢至快

EaseIn 由慢至快。

EaseOut 由快至慢

EaseInOut 由慢至快再由快至慢。

EaseSineIn 由慢至快

EaseSineOut 由快至慢

EaseSineInOut 由慢至快再由快至慢。

EaseExponentialIn 由慢至極快。

EaseExponentialOut 由極快至慢。

EaseExponentialInOut 由慢至極快再由極快至慢。

Speed 人工設(shè)定速度,還可通過 SetSpeed 不斷調(diào)整。

擴展動作

我們已經(jīng)掌握了執(zhí)行各種各樣的動作,也可以按照不同的快慢修改動作執(zhí)行的時間, Cocos2D-iPhone 還提供了針對現(xiàn)有動作的擴展,以實現(xiàn)各種靈活的效果。

延時動作 – Delay在動作序列中增加一個時間間歇:

  1. (void) OnDelay:(id) sender {    
  2.  id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];    
  3.  id ac2 = [ac1 reverse];    
  4. // 實現(xiàn)一個等待間歇    
  5. [spriterunAction:[Sequenceactions:ac1,[DelayTime actionWithDuration:1], ac2, nil]];     
  6. }  

函數(shù)調(diào)用 

函數(shù)在動作序列中間或者結(jié)束調(diào)用某個函數(shù),執(zhí)行任何需要執(zhí)行的任務(wù):動作、狀態(tài)修改等。代碼如下:

  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. }  

對應(yīng)的函數(shù)為:(再做一個動作,這就實現(xiàn)了動作、動作序列的任意擴展和連接)

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

帶對象參數(shù) 調(diào)用自定義函數(shù)時,傳遞當(dāng)前對象。

  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.  }  

對應(yīng)的自定義函數(shù):(這里,我們直接使用了該對象)

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

帶對象、數(shù)據(jù)參數(shù)調(diào)用自定義函數(shù)時,傳遞當(dāng)前對象和一個常量(也可以是指針)。

  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.  }  

對應(yīng)的自定義函數(shù),我們使用了傳遞的對象和數(shù)據(jù):

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

小結(jié):Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (下)的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

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-02 15:47:28

Cocos2D Animation

2011-08-09 16:25:16

Cocos2d視圖坐標(biāo)

2011-07-29 18:02:06

2011-08-17 15:04:48

Cocos2DUIAccelerom加速計

2011-07-27 10:13:23

Cocos2D iPhone

2011-08-11 14:32:04

iPhone游戲Cocos2dActions

2012-06-01 10:27:44

Cocos2d觸摸分發(fā)原理

2011-07-26 10:42:00

Cocoa Cocoa2d 游戲

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2011-08-08 17:17:55

Cocos2D 坐標(biāo) OpenglES

2011-07-27 14:48:21

iPhone Cocos2D 坐標(biāo)

2011-08-08 11:26:39

Cocos2d 游戲 Class類

2011-07-27 13:44:08

2011-07-20 14:04:46

Cocos2d iPhone 游戲

2011-08-08 11:40:42

Cocos2d CCLayer Touch
點贊
收藏

51CTO技術(shù)棧公眾號