Cocos2dx 3.0 過渡篇(十五)幀動畫的存儲播放
前兩篇博文用惡搞的形式寫了action的相關使用,這算是自己的一種嚐試,也可以說是心情的發泄。本篇是action三板斧的最後一板,經過深思熟慮後,我決定就用正常人的方式寫吧...
這次主要介紹動畫的創建即存儲,要實現的功能有:
1、創建四組動畫,並存放至Array中;
2、創建四個虛擬方向按鈕,控製精靈移動與播放相應方向的動畫;
----------------------------------------------------------------------------------------------------------------------------------------------
實現過程如下:
1、事前準備:宏定義每幀精靈圖片的寬高,用枚舉事先確定四個方向對應的tag
- #define BIRD_WIDTH 186 //寬
- #define BIRD_HIGHT 150 //高
- enum dirTag
- {
- Tag_left = 0,
- Tag_down,
- Tag_up,
- Tag_right,
- };
- anim_array = Array::create();//存放四組方向動畫的數組
- anim_array->retain();//記得retain
- bird = Sprite::create("bird.png",Rect(0,0,186,150));//創建一隻鳥人
- bird->setPosition(Point(350,400));
- this->addChild(bird,1);
因為該圖片是沒有對應的plist文件,所以需要將每張圖片分別從大圖中 切割 出來。已知每張圖片的寬為186,高為150.
- SpriteFrame* frame = NULL;
- auto frameArray = Array::create();
- for(int i=0;i<4;i++)
- {
- for(int j=0;j<4;j++)
- {
- frame = SpriteFrame::create("bird.png",Rect(j*BIRD_WIDTH,i*BIRD_HIGHT,BIRD_WIDTH,BIRD_HIGHT));
- frameArray->addObject(frame);
- }
- auto animation = Animation::createWithSpriteFrames(frameArray,0.15f);//0.15s一幀
- auto animate = Animate::create(animation);
- frameArray->removeAllObjects();//清楚數組,為下一組動畫做準備
- anim_array->addObject(animate);//將改組動畫放入anim_array中
- }
- //創建四個精靈,作為控製方向的按鈕,並設置相應的tag
- auto left_btn = Sprite::create("CloseNormal.png");
- left_btn->setPosition(Point(370,150));
- left_btn->setTag(Tag_left);
- this->addChild(left_btn,1);
- auto right_btn = Sprite::create("CloseNormal.png");
- right_btn->setPosition(Point(300,150));
- right_btn->setTag(Tag_right);
- this->addChild(right_btn,1);
- auto up_btn = Sprite::create("CloseNormal.png");
- up_btn->setPosition(Point(335,185));
- up_btn->setTag(Tag_up);
- this->addChild(up_btn,1);
- auto down_btn = Sprite::create("CloseNormal.png");
- down_btn->setPosition(Point(335,115));
- down_btn->setTag(Tag_down);
- this->addChild(down_btn,1);
- auto listener = EventListenerTouchOneByOne::create();//創建一個觸摸監聽(單點觸摸)
- listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定觸摸的回調函數
- listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
- listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//將listener放入事件委托中
- bool HelloWorld::onTouchBegan(Touch* touch, Event *event)
- {
- auto beginPos = touch->getLocationInView();//獲得觸摸位置
- beginPos = Director::getInstance()->convertToGL(beginPos);//坐標轉換
- for(int i=0;i<4;i++)
- {
- auto dir = (Sprite*)this->getChildByTag(i);//通過tag提取方向按鈕
- if(dir->getBoundingBox().containsPoint(beginPos))//如果觸點在按鈕中
- {
- MoveBy* moveBy;
- //判斷在哪個方向
- if(i == Tag_left)
- {
- moveBy = MoveBy::create(0.01f,Point(5,-5));
- }
- else if(i == Tag_right)
- {
- moveBy = MoveBy::create(0.01f,Point(-5,5));
- }
- else if(i == Tag_up)
- {
- moveBy = MoveBy::create(0.01f,Point(5,5));
- }
- else if(i == Tag_down)
- {
- moveBy = MoveBy::create(0.01f,Point(-5,-5));
- }
- auto animate = (Animate*)anim_array->getObjectAtIndex(i);//找出動畫數組中的該組動畫
- auto spaw = Spawn::create(animate,moveBy,NULL);
- bird->runAction(RepeatForever::create(spaw));//如果按鈕一直按著,那麼bird將一直飛下去
- break;
- }
- }
- return true;//返回true表示接收觸摸事件
- }
- void HelloWorld::onTouchMoved(Touch* touch, Event *event)
- {
- }
- void HelloWorld::onTouchEnded(Touch* touch, Event *event)
- {
- bird->stopAllActions();//鬆開按鈕後停止所有動作
- }
恩,實現過程就是這樣啦,挺容易的。
因為重點是控製精靈播放動畫,所以我這裏沒有做如果當鳥人飛出場景後的處理。
轉載請注明來自star特530:https://blog.csdn.net/start530/article/details/20561105
最後更新:2017-04-03 12:56:13