原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/8272388
上篇中实现的是Cocos2d-x提供给我们的动画,这次要实现的动画是自定义的动画。
这次实现的是让一个精灵一直执行我给的图片,从而达到动画的效果,还是截几张图看看。
图片资源来自解压《捕鱼达人》和《魔塔》
一共有18张图片。
CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png"); CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32)); CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32)); CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(64,0,32,32)); CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32)); CCArray *animFrames=CCArray::create(); CC_BREAK_IF(!animFrames); animFrames->addObject(frame0); animFrames->addObject(frame1); animFrames->addObject(frame2); animFrames->addObject(frame3); CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f); CC_BREAK_IF(!animation); CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0); CC_BREAK_IF(!heroSprite0); heroSprite0->setPosition(ccp(100,100)); addChild(heroSprite0,1); CCAnimate *animate=CCAnimate::create(animation); heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去
上面的动画是使英雄一直执行走动下去。
还有另外的一种方法来实现自定义动画:
CCAnimation* animation2 = CCAnimation::create(); for(int i=1;i<19;i++) { char *tt=new char[3]; memset(tt,0,3); std::string s; if(i<10) { itoa(i,tt,10); s="fish00"+std::string(tt); } else { itoa(i,tt,10); s="fish0"+std::string(tt); } s=s+".png"; CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(s.c_str()); animation2->addSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 100, 100))); delete []tt; } animation2->setDelayPerUnit(0.2f); CCAnimate* action = CCAnimate::create(animation2); CCTexture2D *playerRunTexture0 = CCTextureCache::sharedTextureCache()->addImage("fish001.png"); CCSprite *p=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture0, cocos2d::CCRectMake(0, 0, 100, 100))); p->setPosition(ccp(200,200)); addChild(p,1); p->runAction(CCRepeatForever::create(action));
代码是如此的简单,我就不多说了。
最后照例贴出init函数的全部代码:
bool HelloWorld::init() { mPercentage=100; bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png"); CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32)); CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32)); CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(64,0,32,32)); CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32)); CCArray *animFrames=CCArray::create(); CC_BREAK_IF(!animFrames); animFrames->addObject(frame0); animFrames->addObject(frame1); animFrames->addObject(frame2); animFrames->addObject(frame3); CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f); CC_BREAK_IF(!animation); CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0); CC_BREAK_IF(!heroSprite0); heroSprite0->setPosition(ccp(100,100)); addChild(heroSprite0,1); CCAnimate *animate=CCAnimate::create(animation); heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去 CCActionInterval *en=CCRotateBy::create(5,-360); CCSprite *hua=CCSprite::create("end.png"); hua->setPosition(ccp(240,160)); addChild(hua,1); hua->runAction(CCRepeatForever::create(en)); CCAnimation* animation2 = CCAnimation::create(); for(int i=1;i<19;i++) { char *tt=new char[3]; memset(tt,0,3); std::string s; if(i<10) { itoa(i,tt,10); s="fish00"+std::string(tt); } else { itoa(i,tt,10); s="fish0"+std::string(tt); } s=s+".png"; CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(s.c_str()); animation2->addSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 100, 100))); delete []tt; } animation2->setDelayPerUnit(0.2f); CCAnimate* action = CCAnimate::create(animation2); CCTexture2D *playerRunTexture0 = CCTextureCache::sharedTextureCache()->addImage("fish001.png"); CCSprite *p=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture0, cocos2d::CCRectMake(0, 0, 100, 100))); p->setPosition(ccp(200,200)); addChild(p,1); p->runAction(CCRepeatForever::create(action)); bRet = true; } while (0); return bRet; }