您的位置:首页 > 移动开发 > Cocos引擎

How to play animations in Cocos2d-x?

2013-07-06 16:45 363 查看
<a target=_blank href="http://t.cn/RhfSa04">CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("numbers.plist","numbers.png");

CCSprite* sprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("slice2_0_0.png"));
sprite->setPosition(ccp(GameScene::windowSize.width/2,GameScene::windowSize.height/3));

CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("numbers.png");
spriteBatchNode->addChild(sprite);
addChild(spriteBatchNode);
</a>
CCArray* animFrames = CCArray::createWithCapacity(10);

char str[100] = {0};
for(int i = 0; i < 10; ++i)
{
sprintf(str, "slice2_0_%d.png", i);
CCSpriteFrame* frame = cache->spriteFrameByName( str );
animFrames->addObject(frame);
}
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames,1.f);
sprite->runAction(CCAnimate::create(animation) );
or
CCAnimation * anim = CCAnimation::animation();// There are other several ways of storing + adding frames,// this is the most basic using one image per frame.anim->addFrameWithFileName("bear1.png");anim->addFrameWithFileName("bear2.png");anim->addFrameWithFileName("bear3.png");anim->addFrameWithFileName("bear4.png");anim->addFrameWithFileName("bear5.png");anim->addFrameWithFileName("bear6.png");anim->addFrameWithFileName("bear7.png");anim->addFrameWithFileName("bear8.png");CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true);// Duration, animation action and bool to return to frame 1 after finishing.CCSprite *bear = CCSprite::spriteWithFile("bear1.png");addChild(bear,0); //Don't forget to add any sprite you use as a child to the CCLayer!bear->runAction(theAnim);   [/code]
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: