void AnimationCache::parseVersion1(Dictionary* animations)
{
    SpriteFrameCache *frameCache = SpriteFrameCache::sharedSpriteFrameCache();

    DictElement* pElement = NULL;
    CCDICT_FOREACH(animations, pElement)
    {
        Dictionary* animationDict = (Dictionary*)pElement->getObject();
        Array* frameNames = (Array*)animationDict->objectForKey("frames");
        float delay = animationDict->valueForKey("delay")->floatValue();
        Animation* animation = NULL;

        if ( frameNames == NULL ) 
        {
            CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", pElement->getStrKey());
            continue;
        }

        Array* frames = Array::createWithCapacity(frameNames->count());
        frames->retain();

        Object* pObj = NULL;
        CCARRAY_FOREACH(frameNames, pObj)
        {
            const char* frameName = ((String*)pObj)->getCString();
            SpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName);

            if ( ! spriteFrame ) {
                CCLOG("cocos2d: AnimationCache: Animation '%s' refers to frame '%s' which is not currently in the SpriteFrameCache. This frame will not be added to the animation.", pElement->getStrKey(), frameName);

                continue;
            }

            AnimationFrame* animFrame = new AnimationFrame();
            animFrame->initWithSpriteFrame(spriteFrame, 1, NULL);
            frames->addObject(animFrame);
            animFrame->release();
        }

        if ( frames->count() == 0 ) {
            CCLOG("cocos2d: AnimationCache: None of the frames for animation '%s' were found in the SpriteFrameCache. Animation is not being added to the Animation Cache.", pElement->getStrKey());
            continue;
        } else if ( frames->count() != frameNames->count() ) {
            CCLOG("cocos2d: AnimationCache: An animation in your dictionary refers to a frame which is not in the SpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", pElement->getStrKey());
        }

        animation = Animation::create(frames, delay, 1);

        AnimationCache::sharedAnimationCache()->addAnimation(animation, pElement->getStrKey());
        frames->release();
    }    
Exemple #2
0
void Enemy::walkTo(Point position, float time) {
    
    _steppingTileCoord = this->getCave()->getCharacterTileCoordForPosition(this->getNode()->getPosition());
    
    Action *steppingOutAction = Sequence::create(
                                                 DelayTime::create(time / 2),
                                                 CallFunc::create(this, callfunc_selector(Enemy::triggerWeightSwitch)),
                                                 NULL);
    
    SpriteFrameCache *spriteCache = SpriteFrameCache::sharedSpriteFrameCache();
    
    const char *frameName = this->getDirectionName();
    
    Animation *anim = Animation::create();
    
    anim->addSpriteFrame(spriteCache->spriteFrameByName(String::createWithFormat("enemy%s2.png", frameName)->getCString()));
    anim->addSpriteFrame(spriteCache->spriteFrameByName(String::createWithFormat("enemy%s1.png", frameName)->getCString()));
    
    anim->addSpriteFrame(spriteCache->spriteFrameByName(String::createWithFormat("enemy%s3.png", frameName)->getCString()));
    anim->addSpriteFrame(spriteCache->spriteFrameByName(String::createWithFormat("enemy%s1.png", frameName)->getCString()));
    
    anim->setDelayPerUnit(time / anim->getFrames()->count());
    anim->setRestoreOriginalFrame(false);
    
    Action *action = Sequence::create(MoveTo::create(time, position),
                                      CallFunc::create(this, callfunc_selector(Enemy::finishWalk)),
                                      NULL);
    
    this->getNode()->stopAllActions();
    this->getNode()->runAction(Animate::create(anim));
    this->getNode()->runAction(action);
    this->getNode()->runAction(steppingOutAction);
    
    _movementIndex = _movementIndex == 2 ? 3 : 2;
    
}
Exemple #3
0
bool CGameLevel::init()
{
	LayerBack::init();

	g_iGameLevel = 1;
	size = winSize;


	// 选关列表
	SpriteFrameCache *pCache = SpriteFrameCache::sharedSpriteFrameCache();
	pCache->addSpriteFramesWithFile("Image/GameLevel.plist");

	// 选关背景
	SpriteFrame* pBG = pCache->spriteFrameByName("GameLevelBG.png");
	Sprite* pSprite = CCSprite::createWithSpriteFrame(pBG);
	pSprite->setPosition(ccp(size.width/2, size.height/2));
	pSprite->setScale(2.0f);
	pSprite->setRotation(90.0f);
	this->addChild(pSprite, 0);
	_pSprite = pSprite;
		
	/**背景特效	*/
	ParticleSystemQuad *emitter = ParticleSystemQuad::create("Image/Phoenix.plist");
	emitter->setPosition(ccp(size.width/2, size.height/2));
	this->addChild(emitter, 1);

	/**	关卡一按钮	*/
	MenuItemImage *pLevelOne = MenuItemImage::create();
	pLevelOne->setTarget(this, menu_selector(CGameLevel::setLevelMode));
	pLevelOne->setNormalSpriteFrame(pCache->spriteFrameByName("StartLevel1Normal.png")); 
	pLevelOne->setSelectedSpriteFrame(pCache->spriteFrameByName("StartLevel1Selected.png"));
	pLevelOne->setPosition(ccp(size.width*0.14f, size.height/2));
	pLevelOne->_ID = LEVEL_ONE;

	/**	关卡二按钮	*/
	MenuItemImage *pLevelTwo = MenuItemImage::create();
	pLevelTwo->setTarget(this, menu_selector(CGameLevel::setLevelMode));
	pLevelTwo->setNormalSpriteFrame(pCache->spriteFrameByName("StartLevel2Normal.png")); 
	pLevelTwo->setSelectedSpriteFrame(pCache->spriteFrameByName("StartLevel2Selected.png"));
	pLevelTwo->setPosition(ccp(size.width/2, size.height*0.8f));
	pLevelTwo->_ID = LEVEL_TWO;

	/**	关卡三按钮	*/
	MenuItemImage *pLevelThree = MenuItemImage::create();
	pLevelThree->setTarget(this, menu_selector(CGameLevel::setLevelMode));
	pLevelThree->setNormalSpriteFrame(pCache->spriteFrameByName("StartLevel3Normal.png")); 
	pLevelThree->setSelectedSpriteFrame(pCache->spriteFrameByName("StartLevel3Selected.png"));
	pLevelThree->setPosition(ccp(size.width*0.86f, size.height/2));
	pLevelThree->_ID = LEVEL_THREE;

	/**	游戏开始按钮	*/
	MenuItemImage *pStart = MenuItemImage::create();
	pStart->setTarget(this, menu_selector(CGameLevel::setLevelStart));
	pStart->setNormalSpriteFrame(pCache->spriteFrameByName("GameStartNormal.png")); 
	pStart->setSelectedSpriteFrame(pCache->spriteFrameByName("GameStartSelected.png"));
	pStart->setPosition(ccp(size.width/2, size.height*0.2f));

	/**	菜单	*/
	_menu->addChild(pLevelOne);
	_menu->addChild(pLevelTwo);
	_menu->addChild(pLevelThree);
	_menu->addChild(pStart);
	_menu->setPosition(ccp(0, 0));
	_menu->setZOrder(3);

	/**	选中特效	*/
	m_pSun = ParticleSun::create();
	pSprite->addChild(m_pSun, 1);
	m_pSun->setTexture(TextureCache::sharedTextureCache()->addImage("Image/Fire.png") );
	m_pSun->setPosition(_pSprite->convertToNodeSpace(ccp(size.width*0.14f, size.height / 2)));
	m_pSun->setStartSize(30);

	_menuItem->setTarget(this, menu_selector(CGameLevel::back));
	//_menuItem死活不出现

	return true;
}
void SetGameCacheController::setGameCache(){
	AnimationCache* animationCache = AnimationCache::getInstance();
	SpriteFrameCache * cache = SpriteFrameCache::getInstance();

	cache->addSpriteFramesWithFile(Constant::getEnemyGoblinPath());
	cache->addSpriteFramesWithFile(Constant::getEnemySoldierPath());
	cache->addSpriteFramesWithFile(Constant::getEnemyArrowEnemyPath());
	cache->addSpriteFramesWithFile(Constant::getEnemySheepPath());
	cache->addSpriteFramesWithFile(Constant::getEnemyBossAttackPath());
	cache->addSpriteFramesWithFile(Constant::getEnemyBossPath());
	//Goblin Move
	Vector <SpriteFrame*> temp;
	char name[20];
	memset(name, 0, sizeof(name));
	for (int i = 0; i < 5; i++){
		sprintf(name, "Goblin_%d.png", i + 1);
		SpriteFrame* sf = cache->spriteFrameByName(name);
		temp.pushBack(sf);
	}

	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyGoblinMove());

	//Sheep Move
	temp.clear();
	memset(name, 0, sizeof(name));
	for (int i = 4; i < 8; i++){
		sprintf(name, "Sheep_%d.png", i);
		SpriteFrame* sf = cache->spriteFrameByName(name);
		temp.pushBack(sf);
	}

	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemySheepMove());

	//Soldier Move
	temp.clear();
	memset(name, 0, sizeof(name));
	for (int i = 0; i < 8; i++){
		sprintf(name, "Soldier_%d.png", i + 1);
		SpriteFrame* sf = cache->spriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemySoldierMove());

	//ArrowEnemy Attack
	temp.clear();
	for (int i = 9; i < 16; i++){
		sprintf(name, "ArrowEnemy_%d.png", i + 1);
		SpriteFrame* sf = cache->spriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.2f), Constant::getEnemyArrowEnemyAttack());

	//Goblin Death
	temp.clear();
	for (int i = 10; i < 14; i++){
		sprintf(name, "Goblin_%d.png", i + 1);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemyGoblinDeath());

	//Sheep Death
	temp.clear();
	for (int i = 13; i < 22; i++){
		sprintf(name, "Sheep_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemySheepDeath());


	//Soldier Death
	temp.clear();
	for (int i = 17; i < 23; i++){
		sprintf(name, "Soldier_%d.png", i + 1);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemySoldierDeath());

	//ArrowEnemy Death
	temp.clear();
	for (int i = 17; i < 21; i++){
		sprintf(name, "ArrowEnemy_%d.png", i + 1);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemyArrowEnemyDeath());

	//Boss Normal
	temp.clear();
	for (int i = 17; i < 23; i++){
		sprintf(name, "Boss_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.2f), Constant::getEnemyBossNormal());


	//Boss Attack
	temp.clear();
	for (int i = 1; i < 17; i++){
		sprintf(name, "Boss_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyBossAttack());

	//Boss Call

	temp.clear();
	for (int i = 23; i < 58; i++){
		sprintf(name, "Boss_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyBossCall());

	//BossAttack Real
	temp.clear();
	for (int i = 1; i < 20; i++){
		sprintf(name, "BossAttack_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyBossAttackReal());

	//BossAttack Call
	temp.clear();
	for (int i = 32; i < 37; i++){
		sprintf(name, "BossAttack_%d.png", i);
		SpriteFrame* sf = cache->getSpriteFrameByName(name);
		temp.pushBack(sf);
	}
	animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyBossAttackCall());

	
}