使用CCSpriteBatchNode实现动画效果

发布时间:2020-08-03 11:35:24 作者:杜甲同学
来源:网络 阅读:3463

使用CCSpriteBatchNode实现动画效果



//每当屏幕绘制一个纹理的时候,就要经过三个工作:准备渲染、渲染图形和渲染后的清除。这些工作产生了渲染一个纹理所固有的开销,所以要让硬件知道需要渲染的这一组精灵或动作帧,使用的是同一个纹理。这样,图形硬件就会为这组精灵只进行一次准备、渲染、和清除工作了。使用CCSpriteBatchNode就可以做到这一点。

//CCSpriteBatchNode的工作原理如下:

//1、创建一个CCSpriteBatchNode对象,通过传递一个包含所有精灵图片的纹理图册的名字作为参数,并把它加入到当前场景中。

//2、接下来,从纹理图册中创建的任何精灵,应该把它当作CCSpriteBatchNode的一个孩子加进去。只要精灵包含在纹理图册中,那么就没问题,否则会出错。

//3CCSpriteBatchNode可以智能地遍历它所有孩子结点,并通过一次渲染工作来渲染这些孩子,而不是每个精灵都进行一次渲染,这样渲染速度就会更快了



我们在boolHelloWorld::init()添加如下代码:

if ( !CCLayer::init() )
    {
        return false;
    }
                       
                       
                       
    CCAnimation* animation = CCAnimation::create();
                       
                       
    CCSpriteBatchNode* node = CCSpriteBatchNode::create("grossini.png");
    this->addChild(node);
                       
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("grossini.plist");
                       
    for (int i = 1; i < 15; i++)
    {
        CCString* str = CCString::createWithFormat("grossini_dance_%02d.png",i);
        const char* ch = str->getCString();
        CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
        CCSpriteFrame* frame = cache->spriteFrameByName(ch);
        animation->addSpriteFrame(frame);
    }
                       
    animation->setDelayPerUnit(0.3f);
    CCAnimate* animate = CCAnimate::create(animation);
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
                       
    CCSprite* sprite = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("grossini_dance_01.png"));
    sprite->setPosition(ccp(100, 100));
                       
    node->addChild(sprite);
                       
    sprite->runAction(CCRepeatForever::create(animate));


代码下载例子:Hi,推荐文件给你 "动画例子4.zip" http://vdisk.weibo.com/s/Hw4G7

http://pan.baidu.com/share/link?shareid=260459325&uk=3189484501





推荐阅读:
  1. 怎么使用纯CSS实现文本淡入动画效果
  2. 使用vue路由怎么实现前进后退动画效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cocos2d-x ccspritebatchnode

上一篇:Zookeeper基本知识有哪些

下一篇:大数据学习有哪些基础知识

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》