cocos2d裏的動做收集

 
一般調用某個動做的方法:  
 
1 // 建立動做
2  id actionTo = [CCMoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];
3  
4 // 使用動做。(說明:tamara 是一個 CCSprite。)
5 [tamara runAction: actionTo];
 

瞬時動做
顧名思義。瞬時動做就是不須要時間,立刻就完成的動做。瞬時動做的共同基類是 InstantAction。
 
放置 – Place 
效果相似於 node.Position = ccp(x, y)。之因此做爲一個動做來實現是爲了能夠與其餘動做造成一個連續動做。
示例:
1 - (void) OnPlaceMenue:(id) sender {
2 CGSize s = [[CCDirector sharedDirector] winSize];
3 CGPoint p = ccp(CCRANDOM_0_1() * s.width, CCRANDOM_0_1() * s.height);
4 [sprite runAction:[CCPlace actionWithPosition:p]];
5 }
 
 隱藏 – Hide
效果相似於 [node setVisible:NO]. 
示例:
1 - (void) OnHideMenue:(id) sender {
2 [sprite runAction:[CCHide action]];
3 }
 
顯示 – Show
效果相似於 [node setVisible:YES]. 
示例:
1 - (void) OnShowMenue:(id) sender {
2 [sprite runAction:[CCShow action]];
3 }
可見切換 – ToggleVisibility 
代碼以下:
1 - (void) OnToggleMenue:(id) sender {
2 [sprite runAction:[CCToggleVisibility action]];
3 }
延時動做
延時動做就是動做的完成須要必定時間。所以,actionWithDuration 是延時動做執行時的第一個參數,延時動做的共同基類是 CCIntervalAction。
函數的命名規則:
***To: 意味着運動到指定的位置。 
***By:意味着運動到按照指定癿 x、y 增量的位置。(x、y 能夠是負值)
移動到 – CCMoveTo 
移動– CCMoveBy 
跳躍到 – CCJumpTo
設置終點位置和跳躍癿高度和次數。
跳躍 – CCJumpBy
設置終點位置和跳躍癿高度和次數。 
貝塞爾 – CCBezierBy
支持 3 次貝塞爾曲線:P0-起點,P1-起點切線方向,P2-終點切線方向,P3-終點。
首先設置定 Bezier 參數,而後執行。
放大到 – CCScaleTo
設置放大倍數,是浮點型。
放大 – CCScaleBy 
旋轉到 – CCRotateTo 
旋轉 – CCRotateBy 
閃爍 – CCBlink
設定閃爍次數
色調變化到 – CCTintTo 
色調變換 – CCTintBy 
變暗到 – CCFadeTo 
由無變亮 – CCFadeIn
由亮變無 – CCFadeOut

組合動做
按照必定的次序將上述基本動做組合起來,造成連貫的一套組合動做。組合動做包括以 下幾類:
序列 – CCSequence
主要做用就是線序排列若干個動做,而後按前後次序逐個執行。
01 - (void) OnSequence:(id) sender {
02 CGSize s = [[CCDirector sharedDirector] winSize];
03 CGPoint p = ccp(s.width/2, 50);
04 // 建立5個動做
05 id ac0 = [sprite runAction:[CCPlace actionWithPosition:p]];
06 id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
07 id ac2 = [CCJumpTo actionWithDuration:2 position:ccp(150, 50) height:30 jumps:5];
08 id ac3 = [CCBlink actionWithDuration:2 blinks:3];
09 id ac4 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
10 //將 5 個動做組合爲一個序列,注意不要忘了用 nil 結尾。
11 [sprite runAction:[CCSequence actions:ac0, ac1, ac2, ac3, ac4, ac0, nil]];
12 }
同步 – Spawn Spawn 
做用就是同時並列執行若干個動做,但要求動做都必須是能夠同時執行的。好比:移動式翻轉、變色、變大小等。
須要特別注意的是,同步執行最後的完成時間由基本動做中用時最大者決定。
01 - (void) OnSpawn:(id) sender {
02 CGSize s = [[CCDirector sharedDirector] winSize];
03 CGPoint p = ccp(s.width/2, 50);
04 sprite.rotation = 0;
05 [sprite setPosition:p];
06 // 建立 4 個須要並行的動做,確保動做用時可組合。2 – 2 - (1+1)
07 id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
08 id ac2 = [CCRotateTo actionWithDuration:2 angle:180];
09 id ac3 = [CCScaleTo actionWithDuration:1 scale:4];
10 id ac4 = [CCScaleBy actionWithDuration:1 scale:0.5];
11 id seq = [CCSequence actions:ac3, ac4, nil];
12 // 同步動做和組合動做以造成一個連續的新動做。
13 [sprite runAction:[CCSpawn actions:ac1, ac2, seq, nil]];
14
 
重複有限次數 – Repeate
重複有限的次數的動做示例代碼以下:
01 - (void) OnRepeat:(id) sender {
02 CGSize s = [[CCDirector sharedDirector] winSize];
03 CGPoint p = ccp(s.width/2, 50);
04 sprite.rotation = 0;
05 [sprite setPosition:p];
06 // 建立動做序列
07 id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
08 id ac2 = [CCJumpBy actionWithDuration:2 position:ccp(-400, -200) height:30 jumps:5];
09 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 類一般支持。示例以下:
01 - (void) OnReverse:(id) sender {
02 CGSize s = [[CCDirector sharedDirector] winSize];
03 CGPoint p = ccp(s.width/2, 50);
04 sprite.rotation = 0;
05 [sprite setPosition:p];
06 id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(190, 220)];
07 // 建立某個勱做癿反勱做。
08 id ac2 = [ac1 reverse];
09 [sprite runAction:[CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2]];
10 }
 
動畫 – Animation
動畫就是讓精靈自身的連續執行一段影像,造成模擬運動的效果:行走時動精靈狀態、打鬥時的狀態等。
01 - (void) OnAnimation:(id) sender {
02 CCAnimation *animation = [AtlasAnimation animationWithName:@"flight" delay:0.2f];
03 // 每幀的內容定義。
04 for(int i=0;i<3;i++) {
05 int x= i % 3;
06 [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
07 }
08 // 執行勱畫效果
09 id action = [CCAnimate actionWithAnimation: animation];
10 [sprite runAction:[CCRepeat actionWithAction:action times:10]];
11 }
 
無限重複 – RepeatForever 
RepeatForever 是從 Action 類直接派生的,所以沒法參於序列和同步;自身也沒法反向執行。該類的做用就是無限期執行某個動做或動做序列,直到被中止。
01 - (void) OnRepeatForever:(id) sender {
02 CGSize s = [[Director sharedDirector] winSize];
03 CGPoint p = ccp(100, 50);
04 // 飛行噴火模擬勱畫
05 CCAnimation *animation = [CCAnimation animationWithName:@"flight" delay:0.1f];
06 for(int i=0;i<3;i++)
07 {
08  int x= i % 3;
09 [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
10 }
11 id action = [CCAnimate actionWithAnimation: animation];
12 // 將該動畫做爲精靈的本徵動畫,一直運行。
13 [sprite runAction:[RepeatForever actionWithAction:action]];
14 // 在建立第二個連續無限期動做序列。疊加兩者造成完整效果。
15 ccBezierConfig bezier;
16 sprite.rotation = 0;
17 [sprite setPosition:p];
18 bezier.startPosition = ccp(0,0);
19 bezier.controlPoint_1 = ccp(0, s.height/2);
20 bezier.controlPoint_2 = ccp(300, -s.height/2);
21 bezier.endPosition = ccp(300,100);
22 id ac10 = [CCBezierBy actionWithDuration:3 bezier:bezier];
23 id ac11 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
24 id ac1 = [CCSpawn actions:ac10, [Repeat actionWithAction:ac11 times:4], nil];
25 id ac2 = [CCSpawn actions:[ac10 reverse], [CCRepeat actionWithAction:ac11 times:4], nil];
26 // 第二個無限期連續運勱。
27 [sprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];
28 }
 
速度變化
基本動做和組合動做實現了針對精靈的各類運動、動畫效果的改變,但這樣的改變的速度是不變的,經過 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]]; }
相關文章
相關標籤/搜索