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) );orCCAnimation * 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]
相关文章推荐
- How To Use Animations and Sprite Sheets in Cocos2D
- How to Use Animations and Sprite Sheets in Cocos2D 2.X
- How to play Steam in Ubuntu
- How to schedule task daily + onStart() in Play 2.0.4?
- How to play '.rm' files with RhythmBox in Ubutu?
- How to Play '.rm' files with RhythmBox in Ubuntu?
- How to use pthread in cocos2d-x
- How to check crash log using android ndk in cocos2d-x
- CSC231 How to play a wav file in Ciscussion C
- How to Play, Record, and Edit Videos in iOS
- How to configure logging in Play framework
- How to change the Push and Pop animations in a navigation based app
- 【转】Enable ARC in a Cocos2D Project: The Step-by-Step-How-To-Guide Woof-Woof!
- How to delete all the animations in a presentation
- How to play .rmvb files in Ubuntu
- How to play .rmvb files in Ubuntu
- How to play video with AVPlayerViewController (AVKit) in Swift
- How to make a sprite from URL in cocos2d-x
- How to Play, Record, and Edit Videos in iOS
- 【cocos2d-x IOS游戏开发-城市跑酷13】游戏玩法:友情提示:how to play?