天天看点

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