cocos2d-x中兩種播放精靈動畫的方式

在手機遊戲開發過程中,應用動畫效果是必須的。在cocos2d-x中,如何組織動畫,如下:

1.應用CCSpriteBatchNode,步驟如下:

1.1,使用CCSpriteFrameCache::sharedSpriteFrameCache()加載plist文件

1.2,使用CCSpriteBatchNode::create("Big_0.png", 34 );  函數從本地媒介中加載一個被拼合過的大圖。這種方式有利於節省內存,提高渲染效果。

1.3,使用CCSpriteFrame::frameWithTexture函數,從CCSpriteBatchNode對象中加入一個指定位置,大小的圖片,然後放在一個數組中。

1.4,使用CCAnimation::createWithSpriteFrames,從精靈幀容器創建一個序列幀動畫。

1.5,使用CCAnimate::create(animatio );,從序列幀動畫中創建一個動畫動作,到動作之後,就可以使用這個動作了,然後在UI中,就可以看見這個動畫了。

   例子,如下:

 

//第一步(1.1)

  1.  CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("pd_sprites.plist");
  2.  //pvr.ccz是由TexturePacker圖片處理應用後的大圖片。它將所有的相類的圖片,組成在一起,這樣一次性加載到內在中。
  3. //對機器的內在和性能有比較大的更善。
  4.  _actors = CCSpriteBatchNode::create("pd_sprites.pvr.ccz");
  5.  //消除齲齒
  6. _actors->getTexture()->setAliasTexParameters();
  7. this->addChild(_actors, -5);

//第二步(1.2)

  1. //創建精靈幀存放容器 
  2.         CCArray*    tSpriteFrameArray = CCArray::create(); 
  3.         //for循環進行讀取相應數量的紋理塊信息 
  4.         for(int b = 0 ; b < FrameNum; ++b) 
  5.         { 
  6.             //設置紋理不進行抗鋸齒模糊,像素精細 
  7.             pNewBatchNode->getTexture()->setAliasTexParameters(); 
  8.             //設置從對應紋理塊中讀取出一個精靈幀 
  9.             CCSpriteFrame*  tpSpriteFrame = CCSpriteFrame::frameWithTexture(_actors->getTexture(), tRect, tPackNode.m_bRotated, tOffset, tRect.size); 
  10.             //將精靈幀放入容器。  
  11.             tSpriteFrameArray->addObject(tpSpriteFrame); 
  12.              
  13.         }

//第三步(1.3)

  1.         //從精靈幀容器創建一個序列幀動畫。 
  2.         CCAnimation* animation = CCAnimation::createWithSpriteFrames(tSpriteFrameArray,1.0f);   
  3.         //設置每兩幀間時間間隔爲1秒。  
  4.         animation->setDelayPerUnit(1.0f);   
  5.         //設置動畫結束後仍保留動畫幀信息。  
  6.         animation->setRestoreOriginalFrame(true);   

//第四步(1.4)

  1.        //由這個動畫序列幀創建建動作
  2.         CCAnimate* action = CCAnimate::create(animation);   
  3.         //創建精靈。 
  4.         CCSprite* pSprite = CCSprite::create(); 
  5.         //設置精靈位置。 
  6.         pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 

//第五步(1.5)

  1. //讓演員演示這個動畫。  
  2. pSprite->runAction(action);   

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章