您的位置:首页 > Web前端 > Node.js

使用CCSpriteBatchNode实现动画效果

2013-06-27 07:07 417 查看
使用CCSpriteBatchNode实现动画效果

//每当屏幕绘制一个纹理的时候,就要经过三个工作:准备渲染、渲染图形和渲染后的清除。这些工作产生了渲染一个纹理所固有的开销,所以要让硬件知道需要渲染的这一组精灵或动作帧,使用的是同一个纹理。这样,图形硬件就会为这组精灵只进行一次准备、渲染、和清除工作了。使用CCSpriteBatchNode就可以做到这一点。//CCSpriteBatchNode的工作原理如下://1、创建一个CCSpriteBatchNode对象,通过传递一个包含所有精灵图片的纹理图册的名字作为参数,并把它加入到当前场景中。//2、接下来,从纹理图册中创建的任何精灵,应该把它当作CCSpriteBatchNode的一个孩子加进去。只要精灵包含在纹理图册中,那么就没问题,否则会出错。//3、CCSpriteBatchNode可以智能地遍历它所有孩子结点,并通过一次渲染工作来渲染这些孩子,而不是每个精灵都进行一次渲染,这样渲染速度就会更快了

我们在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息