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


Cocos2dx 3.0 過渡篇(十五)幀動畫的存儲播放

前兩篇博文用惡搞的形式寫了action的相關使用,這算是自己的一種嚐試,也可以說是心情的發泄。
本篇是action三板斧的最後一板,經過深思熟慮後,我決定就用正常人的方式寫吧...


這次主要介紹動畫的創建即存儲,要實現的功能有:
1、創建四組動畫,並存放至Array中;
2、創建四個虛擬方向按鈕,控製精靈移動與播放相應方向的動畫;


----------------------------------------------------------------------------------------------------------------------------------------------
實現過程如下:

1、事前準備:宏定義每幀精靈圖片的寬高,用枚舉事先確定四個方向對應的tag

  1. #define BIRD_WIDTH  186   //寬  
  2. #define BIRD_HIGHT  150   //高  
  3.   
  4. enum dirTag  
  5. {  
  6.     Tag_left = 0,  
  7.     Tag_down,  
  8.     Tag_up,  
  9.     Tag_right,  
  10. };  
2、初始化存放動畫的數組,並創建一隻鳥人..

  1. anim_array = Array::create();//存放四組方向動畫的數組  
  2. anim_array->retain();//記得retain  
  3.   
  4.   
  5. bird = Sprite::create("bird.png",Rect(0,0,186,150));//創建一隻鳥人  
  6. bird->setPosition(Point(350,400));  
  7. this->addChild(bird,1);  
3、獲取每幀的圖片並做成動畫。圖片如下:


因為該圖片是沒有對應的plist文件,所以需要將每張圖片分別從大圖中 切割 出來。已知每張圖片的寬為186,高為150.

  1. SpriteFrame* frame = NULL;  
  2. auto frameArray = Array::create();  
  3. for(int i=0;i<4;i++)  
  4. {  
  5.     for(int j=0;j<4;j++)  
  6.     {  
  7.         frame = SpriteFrame::create("bird.png",Rect(j*BIRD_WIDTH,i*BIRD_HIGHT,BIRD_WIDTH,BIRD_HIGHT));  
  8.         frameArray->addObject(frame);  
  9.     }  
  10.   
  11.     auto animation = Animation::createWithSpriteFrames(frameArray,0.15f);//0.15s一幀  
  12.     auto animate = Animate::create(animation);  
  13.     frameArray->removeAllObjects();//清楚數組,為下一組動畫做準備  
  14.     anim_array->addObject(animate);//將改組動畫放入anim_array中  
  15. }  
4、創建四個虛擬按鈕
  1. //創建四個精靈,作為控製方向的按鈕,並設置相應的tag  
  2. auto left_btn = Sprite::create("CloseNormal.png");  
  3. left_btn->setPosition(Point(370,150));  
  4. left_btn->setTag(Tag_left);  
  5. this->addChild(left_btn,1);  
  6.   
  7. auto right_btn = Sprite::create("CloseNormal.png");  
  8. right_btn->setPosition(Point(300,150));  
  9. right_btn->setTag(Tag_right);  
  10. this->addChild(right_btn,1);  
  11.   
  12. auto up_btn = Sprite::create("CloseNormal.png");  
  13. up_btn->setPosition(Point(335,185));  
  14. up_btn->setTag(Tag_up);  
  15. this->addChild(up_btn,1);  
  16.   
  17. auto down_btn = Sprite::create("CloseNormal.png");  
  18. down_btn->setPosition(Point(335,115));  
  19. down_btn->setTag(Tag_down);  
  20. this->addChild(down_btn,1);  
5、因為是用精靈作為按鈕,所以需要添加相應的觸摸監聽事件
  1. auto listener = EventListenerTouchOneByOne::create();//創建一個觸摸監聽(單點觸摸)  
  2. listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定觸摸的回調函數  
  3. listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);  
  4. listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);  
  5. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//將listener放入事件委托中  
6、最後就是觸摸按鈕後的響應函數啦。我是在按下去後bird 播放動畫,鬆開後bird停止播放動畫。這兩個過程分別在touchBegan()和touchEnded()中實現

  1. bool HelloWorld::onTouchBegan(Touch* touch, Event  *event)  
  2. {  
  3.     auto beginPos = touch->getLocationInView();//獲得觸摸位置  
  4.     beginPos = Director::getInstance()->convertToGL(beginPos);//坐標轉換  
  5.   
  6.     for(int i=0;i<4;i++)  
  7.     {  
  8.         auto dir = (Sprite*)this->getChildByTag(i);//通過tag提取方向按鈕  
  9.         if(dir->getBoundingBox().containsPoint(beginPos))//如果觸點在按鈕中  
  10.         {  
  11.             MoveBy* moveBy;  
  12.             //判斷在哪個方向  
  13.             if(i == Tag_left)  
  14.             {  
  15.                 moveBy = MoveBy::create(0.01f,Point(5,-5));  
  16.             }  
  17.             else if(i == Tag_right)  
  18.             {  
  19.                 moveBy = MoveBy::create(0.01f,Point(-5,5));  
  20.             }  
  21.             else if(i == Tag_up)  
  22.             {  
  23.                 moveBy = MoveBy::create(0.01f,Point(5,5));  
  24.             }  
  25.             else if(i == Tag_down)  
  26.             {  
  27.                 moveBy = MoveBy::create(0.01f,Point(-5,-5));  
  28.             }  
  29.   
  30.             auto animate = (Animate*)anim_array->getObjectAtIndex(i);//找出動畫數組中的該組動畫  
  31.             auto spaw = Spawn::create(animate,moveBy,NULL);  
  32.             bird->runAction(RepeatForever::create(spaw));//如果按鈕一直按著,那麼bird將一直飛下去  
  33.             break;  
  34.         }  
  35.     }  
  36.   
  37.     return true;//返回true表示接收觸摸事件  
  38. }  
  39.   
  40. void HelloWorld::onTouchMoved(Touch* touch, Event  *event)  
  41. {  
  42. }  
  43.   
  44. void HelloWorld::onTouchEnded(Touch* touch, Event  *event)  
  45. {  
  46.     bird->stopAllActions();//鬆開按鈕後停止所有動作  
  47. }  

恩,實現過程就是這樣啦,挺容易的。


因為重點是控製精靈播放動畫,所以我這裏沒有做如果當鳥人飛出場景後的處理。


轉載請注明來自star特530:https://blog.csdn.net/start530/article/details/20561105

最後更新:2017-04-03 12:56:13

  上一篇:go cocos2d-x 3.0 bata for android環境配置
  下一篇:go Memcache,Redis,MongoDB(數據緩存係統)方案對比與分析