天天看點

cocos2d-x 菜鳥學習筆記九(引用plist快速生成動畫精靈)

前面說過,在繪制精靈時最好使用CCSpriteBatchNode和CCSpriteFrameCache,前面隻說過可以通過圖檔合并後使用.plist檔案在程式内部進行引用,這回就具體說說這些檔案是如何生成并使用的。這樣的紋理圖檔打包工具應該是不隻一個的,我常用的就是Texture packerGUI,好東西總會有盜版,用之……

cocos2d-x 菜鳥學習筆記九(引用plist快速生成動畫精靈)

要說為什麼要打包使用紋理圖檔的話,那是因為可以節省載入次數,這樣加載效率就高了,然後還有一點就是OPenGL ES1.0需要紋理圖檔以2的n次方的尺寸載入,非常麻煩,是以使用這樣的工具是非常有必要的。

這個工具可以根據不同的遊戲引擎使用最适合的打包方式比如:蘋果專用的圖檔格式PVR。簡單的操作就一句話:點選add Sprites按鈕添加一堆精靈圖檔,設定dataFile導出到的目錄、圖檔格式及imageFormat品質,最後publish即可……具體操作就直接看别人的教程吧:

原帖:http://blog.csdn.net/vagrxie/article/details/6715416

圖檔沒挂的帖:http://www.ituring.com.cn/article/details/52

導出後會看到一張整合的圖檔及對應的pList,至于pList裡都蒇了些什麼,自己用詞本打開就可以發現裡面的秘密了。現在就可以在項目中引用資源了,這裡使用修改後的官方原例中的代碼生成一組帶動畫的精靈吧:

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

	void addNewSprite(int id);
        //用來引用精靈幀的檔案名文本
        char szName[100];
	cocos2d::CCSize size;
	cocos2d::CCSpriteBatchNode* BatchNode1;

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};
           
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //
        // super init first
        //

        CC_BREAK_IF(! CCLayer::init());

        //
        // add your codes below...
        //

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        size = CCDirector::sharedDirector()->getWinSize();

    //預加載紋理
    CCTextureCache::sharedTextureCache()->addImage("grossini.png");	
	//建立繪制批處理節點
	BatchNode1 = CCSpriteBatchNode::create("grossini.png", 50);
    addChild(BatchNode1, 0, 1);
    //這裡的精靈幀緩存添加第二個參數實際上是使用了預加載的紋理緩存中的圖檔
	//也就是說第二個參數可以說是 CCTexture2D而不是單純的textureFileName
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("grossini.plist","grossini.png");

	    setTouchEnabled( true );
        //初始化随機數
	    srand((int)time(0));
    //這裡循環生成了30個精靈
    for(int i=0;i<30;i++){	
        addNewSprite(i+1);
	}
        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void HelloWorld::addNewSprite(int id)
{
    CCPoint p = ccp( CCRANDOM_0_1() * size.width, CCRANDOM_0_1() * size.height);

    int idx = CCRANDOM_0_1() * 1400 / 100;
    int x = (idx%5) * 85;
    int y = (idx/5) * 121;
	//随機取一張紋理圖
    sprintf(szName, "grossini_dance_%02d.png", (rand()%14)+1);
    //從精靈幀緩存中取紋理建立精靈,然後加入繪制節點
	//由于精靈幀紋理圖與batchNode的紋理圖是一緻的,是以不會報錯,而且會加快取紋理和繪制的速度
    CCSprite* sprite = CCSprite::createWithSpriteFrameName(szName);
    BatchNode1->addChild(sprite,1,id);
    
    sprite->setPosition( p );
    //讓精靈随機運作一組動畫
    CCActionInterval* action;
    float random = CCRANDOM_0_1();
    
    if( random < 0.20 )
        action = CCScaleBy::create(3, 2);
    else if(random < 0.40)
        action = CCRotateBy::create(3, 360);
    else if( random < 0.60)
        action = CCBlink::create(1, 3);
    else if( random < 0.8 )
        action = CCTintBy::create(2, 0, -255, -255);
    else 
        action = CCFadeOut::create(2);
	//設定動畫麼向播放
    CCActionInterval* action_back = action->reverse();
    CCActionInterval* seq = CCSequence::create(action, action_back, NULL);
    //設定永久循環
    sprite->runAction( CCRepeatForever::create(seq) );
}
           

以上就是利用TexturePakagerGUI打包生成的plist加速生成精靈的示例。

源代碼下載下傳:http://download.csdn.net/detail/cyistudio/5462641