1. 程式人生 > >Cocos2d-x開發實例介紹幀動畫使用

Cocos2d-x開發實例介紹幀動畫使用

rect http splay end ram nal cache wtl 布爾

以下我們通過一個實例介紹一下幀動畫的使用。這個實比例如以下圖所看到的,點擊Gobutton開始播放動畫,這時候播放button標題變為Stop,點擊Stopbutton能夠停止播放動畫。

技術分享

以下我們再看看詳細的程序代碼,首先看一下看HelloWorldScene.h文件,它的代碼例如以下:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
 
#include "cocos2d.h"
 
class HelloWorld : public cocos2d::Layer
{
         bool isPlaying; //播放標識                                                                                                  ①
   cocos2d::Sprite* sprite;                                                                                                        ②
public:
 
   static cocos2d::Scene* createScene();
   virtual bool init(); 
   
    voidOnAction(cocos2d::Ref* pSender);                                                                                       ③
   
   CREATE_FUNC(HelloWorld);
 
};
 
#endif // __HELLOWORLD_SCENE_H__

第①行代碼是聲明一個布爾變量isPlaying,用來保存播放狀態,true時候說明正在播放,false時候說明停止播放。第②行代碼cocos2d::Sprite*sprite是聲明一個精靈變量。

第③行聲明了一個函數,用來在選擇不同菜單時候的回調。

HelloWorldScene的實現代碼HelloWorldScene.ccp文件,當中HelloWorld::init()函數代碼例如以下:
bool HelloWorld::init()
{
    if( !Layer::init() )
    {
         returnfalse;
    }
 
    SizevisibleSize = Director::getInstance()->getVisibleSize();
    Pointorigin = Director::getInstance()->getVisibleOrigin();
 
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("run.plist");
 
    autobackground = Sprite::createWithSpriteFrameName("background.png");
    background->setAnchorPoint(Point::ZERO);
    this->addChild(background,0);
 
    sprite= Sprite::createWithSpriteFrameName("h1.png");
    sprite->setPosition(Point(visibleSize.width/2,visibleSize.height /2));
    this->addChild(sprite);
 
    isPlaying= false;
   
         //toggle菜單
    autogoSprite = Sprite::createWithSpriteFrameName("go.png");                                                 ①
    autostopSprite = Sprite::createWithSpriteFrameName("stop.png");                                           ②
    autogoToggleMenuItem = MenuItemSprite::create(goSprite, goSprite);                                    ③
 auto stopToggleMenuItem = MenuItemSprite::create(stopSprite,stopSprite);                            ④
 auto toggleMenuItem = MenuItemToggle::createWithCallback(
                    CC_CALLBACK_1(HelloWorld::OnAction,this),
                          goToggleMenuItem , stopToggleMenuItem, NULL);                                             ⑤
    toggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(930,540)));                   ⑥
   auto mn = Menu::create(toggleMenuItem, NULL);
   mn->setPosition(Point::ZERO);
   this->addChild(mn);
 
    returntrue;
}

上述代碼第①行是創建Gobutton精靈,相應的第③行代碼是創建Gobutton(菜單項)。代碼第②行是創建Stopbutton精靈,相應的第④行代碼是創建Stopbutton(菜單項)。

在第⑤行代碼是創建Go和Stop是兩種狀態切換的開關菜單項。第⑥行代碼是設置開關菜單項的位置。

HelloWorldScene的實現代碼HelloWorldScene.ccp文件,當中HelloWorld::OnAction(Ref*pSender)函數代碼例如以下:

void HelloWorld::OnAction(Ref* pSender)
{
   
    if(!isPlaying) {
 
         ///////////////動畫開始//////////////////////
         Animation*animation = Animation::create();                                                                    ①
         for(int i=1; i<= 4; i++)
         {
             __String*frameName = __String::createWithFormat("h%d.png",i);                                    ②
             log("frameName= %s",frameName->getCString());
             SpriteFrame*spriteFrame = SpriteFrameCache::getInstance()->
                                       getSpriteFrameByName(frameName->getCString());                                  ③
             animation->addSpriteFrame(spriteFrame);                                                                           ④
         }
 
         animation->setDelayPerUnit(0.15f);           //設置兩個幀播放時間                             ⑤
         animation->setRestoreOriginalFrame(true);    //動畫運行後還原初始狀態                   ⑥
 
         Animate*action = Animate::create(animation);                                                                          ⑦
         sprite->runAction(RepeatForever::create(action));                                                         ⑧
         //////////////////動畫結束///////////////////
 
         isPlaying= true;
 
    }else {       
         sprite->stopAllActions();                                                                                               ⑨
         isPlaying= false;
    }
}

上述第①行代碼是創建一個Animation對象,它是動畫對象,然後我們要通過循環將各個幀圖片放到Animation對象中。

第②行是獲得幀圖片的文件名稱,String類型是Cocos2d-x字符串數據類型。第③行代碼是通過幀名創建精靈幀對象,第④行代碼把精靈幀對象加入到Animation對象中。

第⑤行代碼是animation->setDelayPerUnit(0.15f)是設置兩個幀播放時間。我們這個動畫播放是4幀。

第⑥行代碼animation->setRestoreOriginalFrame(true)是動畫運行完畢是否還原到初始狀態。第⑦行代碼是通過一個Animation對象創建Animate對象,第⑧行代碼sprite->runAction(RepeatForever::create(action))是運行動畫動作,無限循環方式。

第⑨行代碼sprite->stopAllActions()停止全部的動作。

《Cocos2d-x實戰 C++卷》現已上線,各大商店均已開售:

京東:http://item.jd.com/11584534.html

亞馬遜:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

當當:http://product.dangdang.com/23606265.html

互動出版網:http://product.china-pub.com/3770734

《Cocos2d-x實戰 C++卷》源代碼及樣章下載地址:

源代碼下載地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1

樣章下載地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

歡迎關註智捷iOS課堂微信公共平臺技術分享

Cocos2d-x開發實例介紹幀動畫使用