天天看點

【iOS-Cocos2d遊戲開發之二十二 】CCSpeed實作動畫進行時慢動作\設定遊戲加減速進行

最近也一直在忙,是以也隻能每周的某一天抽出時間來分享一些知識點給童鞋們,希望童鞋們體諒下~

         那麼廢話不多說了,本篇知識點兩個:

               1.利用CCSpeed當精靈執行CCAnimate動作途中設定其播放的速度;

           2.設定遊戲的速率,讓你自由設定整個遊戲的速度;

        首先介紹第一個知識點:

        對于第一個知識點,精靈執行CCAnimate動作途中設定播放速度,說白一點就是當主角或者怪物播放一套幀動作(動畫)的時候,可能突然受到其他因素影響希望主角或者怪物等動作放慢,也就是慢動作的感覺,那麼這時候我們就需要設定動作的播放速度拉,也就是今天要介紹的CCSpeed這個類;可能Himi這裡哇哇哇的說這麼多還是沒亭台明白吧...=。 = 那麼下面我們來看看代碼等就應該明白了;

         直接上一段代碼如下:

[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];   

        CCSprite*mySprite=[CCSprite spriteWithSpriteFrameName:@"himi1.png"];   

        mySprite.position=ccp(120,150);   

        [self addChild:mySprite];    

        CCAnimation*anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];    

        CCAnimate* animate = [CCAnimate actionWithAnimation:anim];   

        CCSequence *seq = [CCSequence actions:animate,nil];    

        CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq];   

        [mySprite runAction:repeat];    

以上代碼建立一個幀動畫(幀資源都在animationFrames.plist加載到記憶體中了),然後建立一個精靈并讓其永久循環執行這個幀動畫;

  童鞋們想一想,如果在這個永久動作執行後,你想在一個任意時間設定這個動畫播放的速度,那麼就利用CCSpeed來實作了,代碼如下:

//左側正常速度的播放   

CCSprite*mySprite=[CCSprite spriteWithSpriteFrameName:@"himi1.png"];   

mySprite.position=ccp(120,150);   

[self addChild:mySprite];    

CCAnimation*anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];    

CCAnimate* animate = [CCAnimate actionWithAnimation:anim];   

CCSequence *seq = [CCSequence actions:animate,nil];    

//讓你的永久動作放入speed中   

CCSpeed *speed =[CCSpeed actionWithAction:[CCRepeatForever actionWithAction:seq] speed:1.0f];   

[speed setTag:888];//設定tag能任意擷取到其執行個體,并且對其進行操作   

[mySprite runAction:speed];   

這段代碼和第一段代碼不同點就是第二段将CCRepeatForever永久動作又包裝到了CCSpeed中,整個動作等同與交給了CCSpeed來控制了,那麼下面我還設定了[speed setTag:888];這個是留出接口,當你需要設定整個CCSpeed包裝的動作速度的時候利用tag擷取到,這個大家肯定很熟悉,那麼擷取動作方式如下:

CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];   

擷取的時候是你之前runAction的精靈來利用getActionByTag來擷取的!

那麼下面繼續添加代碼,我們讓一個由CCSpeed包裝一個幀動畫并讓精靈執行後的5秒後讓其速度變成原有播放速度的一半,代碼如下:

CCSprite *mySpriteByF =[CCSprite spriteWithSpriteFrameName:@"himi1.png"];   

mySpriteByF.position=ccp(360,150);   

[self addChild:mySpriteByF z:0 tag:66];    

anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];    

animate = [CCAnimate actionWithAnimation:anim];     

seq =[CCSequence actions:animate, nil];   

CCSpeed *speed =[CCSpeed actionWithAction:[CCRepeatForever actionWithAction:seq] speed:1.0f];    

[speed setTag:88];   

[mySpriteByF runAction:speed];   

[self schedule:@selector(slowForHimi) interval:5];  

-(void)slowForHimi{   

    [self unschedule:@selector(slowForHimi)];//解除此選擇器   

    CCSprite*sprite=(CCSprite*)[self getChildByTag:66];   

    CCSpeed *speed=(CCSpeed*)[sprite getActionByTag:88];   

    [speed setSpeed:0.5];//放慢原有速度的0.5倍   

}   

CCSpeed的建立很簡單,那麼設定速率的方法如下:

[CCSpeed* setSpeed:XX];  

這裡的XX參數指的是倍率,傳入1表示原速,大于1表示增快,小于1表示放慢速度~

下面直接給出全部測試項目代碼:

//   

//  HelloWorldLayer.m   

//  SLowAnimationByHimi   

//  Created by 華明 李 on 11-11-21.   

//  Copyright Himi 2011年. All rights reserved.   

// Import the interfaces   

#import "HelloWorldLayer.h"   

#import "CCAnimationHelper.h"   

// HelloWorldLayer implementation   

@implementation HelloWorldLayer   

+(CCScene *) scene   

{   

    // 'scene' is an autorelease object.   

    CCScene *scene = [CCScene node];   

    // 'layer' is an autorelease object.   

    HelloWorldLayer *layer = [HelloWorldLayer node];   

    // add layer as a child to scene   

    [scene addChild: layer];   

    // return the scene   

    return scene;   

//CCJumpTo實作,抛物線   

// on "init" you need to initialize your instance   

-(id) init{   

    if( (self=[super init])) {   

        CCLabelTTF *label = [CCLabelTTF labelWithString:@"暫緩動作&設定整個遊戲加速/減速" fontName:@"Marker Felt" fontSize:24];   

        label.position =  ccp(260,260);   

        [self addChild: label z:0  ];    

        label = [CCLabelTTF labelWithString:@"正常速度的播放" fontName:@"Marker Felt" fontSize:12];   

        label.position =  ccp(120,220);   

        [self addChild: label z:0 tag:99];    

        label = [CCLabelTTF labelWithString:@"左側動态放慢的速度的動作" fontName:@"Marker Felt" fontSize:12];   

        label.position =  ccp(350,220);   

        [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"];   

        //左側正常速度的播放   

        //左側動态放慢的速度的動作    

        CCSprite *mySpriteByF =[CCSprite spriteWithSpriteFrameName:@"himi1.png"];   

        mySpriteByF.position=ccp(360,150);   

        [self addChild:mySpriteByF z:0 tag:66];    

        anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1];    

        animate = [CCAnimate actionWithAnimation:anim];     

        seq =[CCSequence actions:animate, nil];   

        CCSpeed *speed =[CCSpeed actionWithAction:[CCRepeatForever actionWithAction:seq] speed:1.0f];    

        [speed setTag:88];   

        [mySpriteByF runAction:speed];   

        [self schedule:@selector(slowForHimi) interval:5];   

    }   

    return self;   

// on "dealloc" you need to release all your retained objects   

- (void) dealloc   

    // in case you have something to dealloc, do it in this method   

    // in this particular example nothing needs to be released.   

    // cocos2d will automatically release all the children (Label)   

    // don't forget to call "super dealloc"   

    [super dealloc];   

@end   

運作截圖如下:  (圖檔中有個"左"寫錯了,應該是"右",懶得改了,大家知道就行了 娃哈哈)

<a target="_blank" href="http://blog.51cto.com/attachment/201111/220954179.png"></a>

從截圖中可能童鞋們看不出什麼效果。等文章最後放出源碼,大家運作就可以看到效果了-。 -  

    Himi當時做的時候因為用CCSpeed方式有問題一直不行,就去改了源碼弄的。後來才發現CCSpeed正确用法,我去了=。 =

這裡Himi必須強調一點!!!!!!!

    很多時候你的主角的動作利用CCAction來實作,移動則是在update刷幀函數或者一些選擇器的方法中進行的,那麼為了讓你的主角慢動作比較逼真,那麼Himi建議不要使用scheduleUpdate函數,因為這個你無法修改每次調用update的時間預設都是每幀都調用,那麼你應該自己定義一個選擇器當刷邏輯的函數,這樣就能配合CCSpeed實作逼真慢動作拉~

  下面我們來介紹第二個知識點:設定遊戲速度

    對于遊戲速度最常見的出現在塔防遊戲中,當玩家建立好防守的東東後開始出怪後,可能怪物移動速度慢,而玩家着急看到結果,那麼我們就會人性化的加上加快遊戲速度的按鈕拉~那麼這個功能在Cocos2d引擎中封裝好的,一句代碼即可完成,如下代碼即可:

[[CCScheduler sharedScheduler] setTimeScale:XX];   

這裡的XX仍然是倍率:傳入1表示原速,大于1表示增快,小于1表示放慢速度~

      OK,本篇就到此~

本文轉自 xiaominghimi 51CTO部落格,原文連結:http://blog.51cto.com/xiaominghimi/723894,如需轉載請自行聯系原作者

繼續閱讀