Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (下)
Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (上)是本節(jié)介紹的內(nèi)容,繼續(xù)Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (上)的內(nèi)容開始介紹。我們先來看內(nèi)容。
重復(fù)有限次數(shù) – Repeate
重復(fù)有限的次數(shù)的動作示例代碼如下:
- (void) OnRepeat:(id) sender {
- CGSize s = [[CCDirector sharedDirector] winSize];
- CGPoint p = ccp(s.width/2, 50);
- sprite.rotation = 0;
- [sprite setPosition:p];
- // 創(chuàng)建動作序列
- id ac1 = [CCMoveTo actionWithDuration:2 position:ccp(s.width - 50, s.height - 50)];
- id ac2 = [CCJumpBy actionWithDuration:2 position:ccp(-400, -200) height:30 jumps:5];
- id ac3 = [CCJumpBy actionWithDuration:2 position:ccp(s.width/2, 0) height:20 jumps:3];
- id seq = [CCSequence actions:ac1, ac2, ac3, nil];
- //重復(fù)運行上述勱作序列 3 次。
- [sprite runAction:[CCRepeat actionWithAction:seq times:3]];
- }
反動作 – Reverse
反動作就是反向(逆向)執(zhí)行某個動作,支持針對動作序列的反動作序列。反動作不是一個專門的類,而是 CCFiniteAction 引入的一個接口。不是所有的類都支持反動作,XxxxTo 類通常不支持反動作,XxxxBy 類通常支持。示例如下:
- (void) OnReverse:(id) sender {
- 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)];
- // 創(chuàng)建某個勱作癿反勱作。
- id ac2 = [ac1 reverse];
- [sprite runAction:[CCRepeat actionWithAction:[CCSequence actions:ac1, ac2,nil] times:2]];
- }
- 動畫 – Animation
- 動畫就是讓精靈自身的連續(xù)執(zhí)行一段影像,形成模擬運動的效果:行走時動精靈狀態(tài)、打斗時的狀態(tài)等。
- (void) OnAnimation:(id) sender {
- CCAnimation *animation = [AtlasAnimation animationWithName:@"flight" delay:0.2f];
- // 每幀的內(nèi)容定義。
- for(int i=0;i<3;i++) {
- int x= i % 3;
- [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
- }
- // 執(zhí)行勱畫效果
- id action = [CCAnimate actionWithAnimation: animation];
- [sprite runAction:[CCRepeat actionWithAction:action times:10]];
- }
- 無限重復(fù) – RepeatForever
- RepeatForever 是從 Action 類直接派生的,因此無法參于序列和同步;自身也無法反向執(zhí)行。該類的作用就是無限期執(zhí)行某個動作或動作序列,直到被停止。
- (void) OnRepeatForever:(id) sender {
- CGSize s = [[Director sharedDirector] winSize];
- CGPoint p = ccp(100, 50);
- // 飛行噴火模擬勱畫
- CCAnimation *animation = [CCAnimation animationWithName:@"flight" delay:0.1f];
- for(int i=0;i<3;i++)
- {
- int x= i % 3;
- [animation addFrameWithRect: CGRectMake(x*32, 0, 31,30) ];
- }
- id action = [CCAnimate actionWithAnimation: animation];
- // 將該動畫作為精靈的本征動畫,一直運行。
- [sprite runAction:[RepeatForever actionWithAction:action]];
- // 在創(chuàng)建第二個連續(xù)無限期動作序列。疊加二者形成完整效果。
- ccBezierConfig bezier;
- sprite.rotation = 0;
- [sprite setPosition:p];
- bezier.startPosition = ccp(0,0);
- bezier.controlPoint_1 = ccp(0, s.height/2);
- bezier.controlPoint_2 = ccp(300, -s.height/2);
- bezier.endPosition = ccp(300,100);
- id ac10 = [CCBezierBy actionWithDuration:3 bezier:bezier];
- id ac11 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
- id ac1 = [CCSpawn actions:ac10, [Repeat actionWithAction:ac11 times:4], nil];
- id ac2 = [CCSpawn actions:[ac10 reverse], [CCRepeat actionWithAction:ac11 times:4], nil];
- // 第二個無限期連續(xù)運勱。
- [sprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];
- }
速度變化
基本動作和組合動作實現(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在動作序列中增加一個時間間歇:
- (void) OnDelay:(id) sender {
- id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
- id ac2 = [ac1 reverse];
- // 實現(xiàn)一個等待間歇
- [spriterunAction:[Sequenceactions:ac1,[DelayTime actionWithDuration:1], ac2, nil]];
- }
函數(shù)調(diào)用
函數(shù)在動作序列中間或者結(jié)束調(diào)用某個函數(shù),執(zhí)行任何需要執(zhí)行的任務(wù):動作、狀態(tài)修改等。代碼如下:
- (void) OnCallFunc:(id) sender {
- id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
- id ac2 = [ac1 reverse];
- id acf = [CCCallFunc actionWithTarget:self selector:@selector(CallBack1)];
- [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
- }
對應(yīng)的函數(shù)為:(再做一個動作,這就實現(xiàn)了動作、動作序列的任意擴展和連接)
- (void) CallBack1 {
- [sprite runAction:[CCTintBy actionWithDuration:0.5 red:255 green:0 blue:255]];
- }
帶對象參數(shù) 調(diào)用自定義函數(shù)時,傳遞當(dāng)前對象。
- (void) OnCallFuncN:(id) sender {
- id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
- id ac2 = [ac1 reverse];
- id acf = [CallFuncN actionWithTarget:self selector:@selector(CallBack2:)];
- [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
- }
對應(yīng)的自定義函數(shù):(這里,我們直接使用了該對象)
- (void) CallBack2:(id)sender {
- [sender runAction:[CCTintBy actionWithDuration:1 red:255 green:0 blue:255]];
- }
帶對象、數(shù)據(jù)參數(shù)調(diào)用自定義函數(shù)時,傳遞當(dāng)前對象和一個常量(也可以是指針)。
- (void) OnCallFuncND:(id) sender {
- id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
- id ac2 = [ac1 reverse];
- id acf = [CCCallFuncND actionWithTarget:self selector:@selector(CallBack3:data:) data:(void*)2];
- [sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
- }
對應(yīng)的自定義函數(shù),我們使用了傳遞的對象和數(shù)據(jù):
- (void) CallBack3:(id)sender data:(void*)data {
- [sender runAction:[CCTintBy actionWithDuration:(NSInteger)data red:255 green:0 blue:255]]; }
小結(jié):Cocoa學(xué)習(xí)筆記 Cocos2d 各種動作介紹 (下)的內(nèi)容介紹完了,希望本文對你有所幫助!