void SheetAnimations_LoadAnimationTest::executeTestCodeAtPosition(CCPoint p) { GHSprite * sprite = GHSprite::createWithSpriteFrameName("number_0.png");//the name of one of the sprite in the sheet plist if(batchNodeParent != NULL){//if we use batch nodes we must add the sprite to its batch parent batchNodeParent->addChild(sprite); } else{//if we dont use batch nodes then we must add the sprite to a normal node - e.g the layer or another node this->addChild(sprite); } sprite->setPosition(p); CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache(); CCAnimation *animation = cache->animationByName("NumbersAnim");//the name of the animation CCAnimate* action = CCAnimate::create(animation); sprite->runAction(action); // [sprite runAction: [CCSequence actions: action, [action reverse], nil]]; CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(SheetAnimations_LoadAnimationTest::animationFrameNotification), CCAnimationFrameDisplayedNotification, NULL); }
} CCAction* HelloWorld::createAction(int begin,int end,char* cacheActionName,CCPoint point){ CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); CCArray *array = CCArray::createWithCapacity(end-begin); char name[20]; for(int i = begin ;i<end;i++){ sprintf(name,"A1_%d.png",i); CCSpriteFrame* frame =cache->spriteFrameByName(name); array->addObject(frame); } CCAnimation *plistAnimation = CCAnimation::createWithSpriteFrames(array,0.2f); CCAnimationCache::sharedAnimationCache()->addAnimation(plistAnimation, cacheActionName); array->removeAllObjects(); CCAnimation *animation = animCache->animationByName(cacheActionName); animation->setRestoreOriginalFrame(true); CCAnimate *ani=CCAnimate::create(animation); CCActionInterval* plistSeq=(CCActionInterval*)(CCSequence::create(ani, CCFlipX::create(point.x>0? true:false), ani->copy()->autorelease(), NULL ));
std::string SheetAnimations_LoadAnimationTest::initTest() { CCSize s = CCDirector::sharedDirector()->getWinSize(); #if 1 // Use batch node. Faster //when using batches - load a batch node using the generated image batchNodeParent = CCSpriteBatchNode::create("spriteSheetAnimationsTest_Numbers.png", 100); this->addChild(batchNodeParent, 0); #endif CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames(); //load into the sprite frame cache the plist generated by SH CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("spriteSheetAnimationsTest_Numbers.plist"); //multiple animations in this document so lets cache all the sheets //not necessary - but if we dont do this we will have a warning in the console CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("spriteSheetAnimationsTest_robotBlinking.plist"); CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache(); cache->addAnimationsWithFile("spriteSheetAnimationsTest_SheetAnimations.plist");//the animation exported plist file //load into the sprite frame cache the plist generated by SH CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("loadSprites_spritesSheet.plist"); this->executeTestCodeAtPosition(ccp(s.width/2, s.height/2)); return "Demonstrate the use of sprite sheets animations.\nFirst frames will run faster, while last frames will run slower\ndemonstrating variable frame time.\nWhen animation finishes, the initial sprites is restored (Number 0).\nFrame 5 contains user info but Cocos2d-X native\nanimation notifications is not implemented yet as stated in \nCCActionInterval.cpp line 2429.\nCheck next test for notifications."; }
} void HelloWorld::moveDone(){ // sprite->stopAllActions(); CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); CCAnimation *standAnimation = animCache->animationByName("stand"); standAnimation->setRestoreOriginalFrame(true); CCAnimate *standAni=CCAnimate::create(standAnimation); CCActionInterval* s=(CCActionInterval*)(CCSequence::create(standAni, standAni->copy()->autorelease(), NULL )); CCAction *frameAction=CCRepeatForever::create(s);
void HelloWorld::MoveDone() { PlayerIsIdle = true; m_pSprite->stopAllActions(); CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); char name[20]; sprintf(name,"stand%d",dirction); CCAnimation *standAnimation = animCache->animationByName(name); if (standAnimation==NULL) { CCArray * standArray= CCArray::createWithCapacity(1); char standName[20]; int dir ; if (dirction == 6 ) { dir = 2;} else { dir = dirction;} for(int i=7;i<8;i++){ sprintf(standName, "walk_%d_%d.png",dir,i); CCSpriteFrame* frame =cache->spriteFrameByName(standName); standArray->addObject(frame); } standAnimation =CCAnimation::createWithSpriteFrames(standArray,0.2f); animCache->addAnimation(standAnimation, name); standArray->removeAllObjects(); delete standArray; } standAnimation->setRestoreOriginalFrame(true); CCAnimate *standAni=CCAnimate::create(standAnimation); CCActionInterval* s=(CCActionInterval*)(CCSequence::create(standAni, standAni->copy()->autorelease(), NULL )); CCAction *frameAction=CCRepeatForever::create(s); m_pSprite->setFlipX(dirction == 6); m_pSprite->runAction(frameAction); }
//------------------------------------------------------------------------- void CStarWidget::DoEffect(CCPoint&pos,string strEffectName,string strEffectFirstFarmeName) { CCAnimationCache* pAnimeCache = CCAnimationCache::sharedAnimationCache(); if (pAnimeCache == NULL) { return; } CCAnimation* pAnime = pAnimeCache->animationByName(strEffectName.c_str()); if ( pAnime == NULL) { return; } CCAnimate * pAnimate = CCAnimate::create(pAnime); CCSprite* spTemp = CCSprite::createWithSpriteFrameName(strEffectFirstFarmeName.c_str()); spTemp->setAnchorPoint(ccp(0.5,0.5)); addChild(spTemp); spTemp->setPosition(pos); CCFiniteTimeAction* actions= CCSequence::create(pAnimate, CCCallFuncN::create(this,callfuncN_selector(CStarWidget::OnEffectEnd)),NULL); spTemp->runAction(actions); }
void Recipe15::eat(float delta) { CCSprite* player = (CCSprite*) this->getChildByTag(1); player->setTexture(CCTextureCache::sharedTextureCache()->addImage("monkey01.png")); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); this->m_points += player->getPositionX()/(winSize.width/4) + 1; CCLabelTTF* label = (CCLabelTTF*)this->getChildByTag(11); CCString* points = CCString::createWithFormat("%d", this->m_points); label->setString(points->getCString()); CCAnimationCache* cache = CCAnimationCache::sharedAnimationCache(); cache->addAnimationsWithFile("animations.plist"); CCAnimation* animation2 = cache->animationByName("bite"); CCAnimate* action = CCAnimate::create(animation2); player->runAction(action); }
CCAnimation * CCNodeLoader::parsePropTypeAnimation(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) { CCString * animationFile = pCCBReader->readCachedString(); CCString * animation = pCCBReader->readCachedString(); CCAnimation * ccAnimation = NULL; // Support for stripping relative file paths, since ios doesn't currently // know what to do with them, since its pulling from bundle. // Eventually this should be handled by a client side asset manager // interface which figured out what resources to load. // TODO Does this problem exist in C++? animation = CCBReader::lastPathComponent(animation); animationFile = CCBReader::lastPathComponent(animationFile); if (animation != NULL && animation->compare("") != 0) { CCAnimationCache * animationCache = CCAnimationCache::sharedAnimationCache(); animationCache->addAnimationsWithFile(animationFile->getCString()); ccAnimation = animationCache->animationByName(animation->getCString()); } return ccAnimation; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setTouchEnabled(true); CCSize size = CCDirector::sharedDirector()->getWinSize(); CCAnimationCache::purgeSharedAnimationCache(); CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); animCache->addAnimationsWithFile("sg.plist"); cache=CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("sg.plist"); sprite=CCSprite::createWithSpriteFrameName("A1_6.png"); sprite->setPosition(ccp(size.width-sprite->getContentSize().width,size.height/2)); spriteBatchNode=CCSpriteBatchNode::create("sg.png"); spriteBatchNode->addChild(sprite); addChild(spriteBatchNode); cache->addSpriteFramesWithFile("hero.plist"); hero=CCSprite::createWithSpriteFrameName("Hero02_0.png"); hero->setPosition(ccp(hero->getContentSize().width,size.height/2)); heroBatchNode=CCSpriteBatchNode::create("hero.png"); heroBatchNode ->addChild(hero); hero->setFlipX(true); addChild(heroBatchNode); attackArray =CCArray::createWithCapacity(4); char attackName[20]; for(int i=0;i<4;i++){ sprintf(attackName, "A1_%d.png",i); CCSpriteFrame* frame =cache->spriteFrameByName(attackName); attackArray->addObject(frame); } CCAnimation *attackAnimation =CCAnimation::createWithSpriteFrames(attackArray,0.2f); CCAnimationCache::sharedAnimationCache()->addAnimation(attackAnimation, "attack"); attackArray->removeAllObjects(); standArray= CCArray::createWithCapacity(1); char standName[20]; for(int i=6;i<7;i++){ sprintf(standName, "A1_%d.png",i); CCSpriteFrame* frame =cache->spriteFrameByName(standName); standArray->addObject(frame); } CCAnimation *standAnimation =CCAnimation::createWithSpriteFrames(standArray,0.2f); CCAnimationCache::sharedAnimationCache()->addAnimation(standAnimation, "stand"); standArray->removeAllObjects();
void UnitFactory::loadAnimations() { CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache(); cache->addAnimationsWithFile(ANIMATIONS_FILE); }