动画原理:
// // HelloWorldScene.h // #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld); void myUpdate(float dt); private: int currentFramIdx = 0; }; #endif
// // HelloWorldScene.cpp // #include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // 添加4个精灵 CCSprite *pSpr1 = CCSprite::create("crop1.png"); CCSprite *pSpr2 = CCSprite::create("crop2.png"); CCSprite *pSpr3 = CCSprite::create("crop3.png"); CCSprite *pSpr4 = CCSprite::create("crop4.png"); pSpr1->setPosition(ccp(100,180)); pSpr2->setPosition(ccp(100,180)); pSpr3->setPosition(ccp(100,180)); pSpr4->setPosition(ccp(100,180)); pSpr1->setVisible(true); pSpr2->setVisible(false); pSpr3->setVisible(false); pSpr4->setVisible(false); this->addChild(pSpr1,0); this->addChild(pSpr2,1); this->addChild(pSpr3,2); this->addChild(pSpr4,3); this->schedule(schedule_selector(HelloWorld::myUpdate),1.0); return true; } void HelloWorld::myUpdate(float dt) { CCLOG("HelloWorld::myUpdate"); currentFramIdx ++; CCArray *array = this->getChildren(); if (currentFramIdx >= array->count()) { currentFramIdx = 0; } for (int i = 0; i < array->count(); i ++) { CCSprite *pSpr = (CCSprite *)this->getChildByTag(i); pSpr->setVisible(false); } CCSprite *pSpr = (CCSprite *)array->objectAtIndex(currentFramIdx); pSpr->setVisible(true); }
<pre name="code" class="cpp">// // HelloWorldScene.h // #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld); }; #endif
// // HelloWorldScene.cpp // #include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // 手动添加帧序列创建动画 CCSprite *pSpr = CCSprite::create("crop1.png"); pSpr->setPosition(ccp(170,200)); this->addChild(pSpr); CCAnimation *animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("crop1.png"); animation->addSpriteFrameWithFileName("crop2.png"); animation->addSpriteFrameWithFileName("crop3.png"); animation->addSpriteFrameWithFileName("crop4.png"); animation->setDelayPerUnit(1.0f); animation->setRestoreOriginalFrame(true); animation->setLoops(-1); CCActionInterval *animate = CCAnimate::create(animation); pSpr->runAction(animate); // 通过资源文件创建动画 CCTexture2D::PVRImagesHavePremultipliedAlpha(true); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("crop.plist"); CCSprite *pSpr = CCSprite::createWithSpriteFrameName("crop1.png"); pSpr->setPosition(ccp(170,200)); this->addChild(pSpr); CCArray *arrayFrames = CCArray::createWithCapacity(4); char str[100] = {0}; for (int i = 1; i < 5; i ++) { sprintf(str,"crop%d.png",i); CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str); arrayFrames->addObject(frame); } CCAnimation *animation = CCAnimation::createWithSpriteFrames(arrayFrames); animation->setLoops(-1); animation->setDelayPerUnit(1.0f); animation->setRestoreOriginalFrame(true); CCAnimate *animate = CCAnimate::create(animation); pSpr->runAction(animate); CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("crop.plist"); return true; }