cocos2dx 精灵动作优化


精灵的概念这里就不说了,论坛上有很多。


一、今天的问题是:如果我有一套美术资源,如一个羊的素材(当然是png),它有站立 ,有跑步,有行走动作。如果当前要加载两个这样的羊,我们该怎么做?


1.第一种方法,想必大家都知道  加载2次。

当然这样有的话,如果我要100个难道也要加载100次的Animation吗?显然不是

2.第二种方法 这里我就按照自己的理解注释了

#define SHEEP_STAND "sheep_stand"
#define SHEEP_RUN   "sheep_run"
#define SHEEP_BATTACK "sheep_battack"

    //创建帧缓冲
    CCSpriteFrameCache *sheepFrameCache =CCSpriteFrameCache::sharedSpriteFrameCache();
    sheepFrameCache->addSpriteFramesWithFile("sheep.plist");
    //创建动作缓冲  
    CCAnimationCache *sheepAniCache = CCAnimationCache::sharedAnimationCache();

cocos2dxCCAnimationCache是单例  可以直接取到不用创建

        CCAnimationCache *SpriteAni = CCAnimation::create();
        for(int i=1;i<7;i++)
        {
            char szName[100] = {0};
            sprintf(szName, "stand/stand%d.png",i);
            
            CCLog("%s",szName);
            
            CCSpriteFrame *SFrame = sheepFrameCache->spriteFrameByName(szName);
            SpriteAni->addSpriteFrame(SFrame);
        }
        sheepAniCache->addAnimation(SpriteAni, SHEEP_STAND);

这样我就加入到了动作缓冲中 下次要是再使用的时候就可以:

SpriteAni = sheepAniCache->animationByName(SHEEP_STAND);

OK 怎么加载动作的话大家一概自己清楚

        CCFiniteTimeAction *action = CCSequence::create(aniStart, CCAnimate::create(SpriteAni,aniEnd,NULL);

然后runAction就可以了!


二、减少绘制  引入CCSpriteBatchNode类

        CCSpriteBatchNode *parent2 = CCSpriteBatchNode::create("sheep.pvr.ccz");
        addChild(parent2);
        CCSprite *sh1 = CCSprite::create("1.png");
        parent2->addChild(sh1);
        CCSprite *sh2 = CCSprite::create("2.png");
        parent2->addChild(sh2);
        cocos2dx作者解释:

    // Animation using Sprite batch
    //
    // A CCSpriteBatchNode can reference one and only one texture (one .png file)
    // Sprites that are contained in that texture can be instantiatied as CCSprites and then added to the CCSpriteBatchNode
    // All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call
    // If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值