閱讀977 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

下麵我們通過一個實例介紹一下幀動畫的使用這個實例如下圖所示點擊Go按鈕開始播放動畫這時候播放按鈕標題變為Stop點擊Stop按鈕可以停止播放動畫。


下麵我們再看看具體的程序代碼首先看一下看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;
}

上述代碼第①行是創建Go按鈕精靈對應的第③行代碼是創建Go按鈕菜單項。代碼第②行是創建Stop按鈕精靈對應的第④行代碼是創建Stop按鈕菜單項。在第⑤行代碼是創建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++卷》現已上線各大商店均已開售

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

亞馬遜https://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

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

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

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

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

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

歡迎關注智捷iOS課堂微信公共平台

最後更新:2017-04-03 05:39:19

  上一篇:go Bitcask存儲模型
  下一篇:go curl 命令使用