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 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); }