Exemple #1
0
/**
 * Checks the box for potential matches
 * either horizontally or vertically
 */
void Box::checkWith(Orientation orient, int order)
{
    CCLOG("+F Box::checkWith()");
	int iMax = (orient == OrientationHori) ? size.height: size.width;
	int jMax = (orient == OrientationHori) ? size.width: size.height;
	for (int i=0; i<iMax; i++) {
		int count = 0;
		int vv = -1;
		CCArray *matches = CCArray::createWithCapacity(jMax);

		for (int j=0; j<jMax; j++) {
			Tile2 *tile = this->objectAtX(((orient == OrientationVert)? i:j), ((orient == OrientationVert)? j :i));
            
            if(tile->value == 0) {
                readyToRemoveTiles->addObject(tile);
            }
            else if(tile->value == vv){
                count++;
                matches->addObject(tile);
			} else {
                // current streak has been broken
                this->doCombinations(count, matches, orient, order);

				count = 1;
                matches->removeAllObjects();
                matches->addObject(tile);
				vv = tile->value;
			}
        }
        this->doCombinations(count, matches, orient, order);
        matches->removeAllObjects();
        matches->release();
    }
    CCLOG("+F Box::checkWith()");
}
SGAttackInfo SGMonster::attack() {
    atkState = MONSTER_ATK_PREV;
    
    int actWhat = selectAttack();
    
    CCLog("Monster Attack Dir = %d", actWhat);
    
    nowAttackInfo.atk = this->atk;
    nowAttackInfo.atkDir = act_attack[actWhat].atkDir;
    nowAttackInfo.nFrames = act_attack[actWhat].nFrames;
    
    int frame1, frame2;
    frame1 = nowAttackInfo.nFrames / 2;
    if(nowAttackInfo.nFrames%2 == 0) frame2 = frame1;
    else frame2 = frame1 + 1;
    
    monsterSprite->stopAllActions();
    func_attack_first();
    CCArray* pAttackChange = CCArray::create();
    pAttackChange->retain();
    pAttackChange->addObject(CCDelayTime::create(GAME_FRAME_SPEED*(frame1)));
    pAttackChange->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::func_attack_second)));
    pAttackChange->addObject(CCDelayTime::create(GAME_FRAME_SPEED*(frame2)));
    pAttackChange->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::func_attack_end)));
    pAttackChange->addObject(CCDelayTime::create(GAME_FRAME_SPEED));
	monsterSprite->runAction(CCSpawn::create(act_attack[actWhat].act_attack,
                                             CCSequence::create(pAttackChange)));
    pAttackChange->release();
    
	return nowAttackInfo;
}
Exemple #3
0
CCPriest* CCPriest::createWithVoid()
{
	CCPriest* pCCPriest = CCPriest::create();

	//读取plist
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Priests_0.plist", "Priests_0.png");

	//生成站立动画
	CCArray *frames = CCArray::create();
	for( int i=0; i<6; i++ )
	{
		char pngName[30];
		sprintf(pngName, "Priests_0%d.png", i+1);
		CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pngName);
		frames->addObject(frame);
	}
	CCAnimation* pStand = CCAnimation::createWithSpriteFrames(frames, 1./6);
	frames->release();

	pCCPriest->setScaleX(-0.8);
	pCCPriest->setScaleY(0.8);
	pCCPriest->m_pStand = CCAnimate::create(pStand);
	pCCPriest->runAction(CCRepeatForever::create(pCCPriest->m_pStand));

	return pCCPriest;
}
static void array_Value(CCArray * aArray, const cocos2d::ValueVector & value)
{
    cocos2d::ValueVector::const_iterator beg = value.begin();
    cocos2d::ValueVector::const_iterator end = value.end();
    for (; beg != end; ++beg)
    {
        const Value & v = *beg;
        if (v.getType() == Value::Type::MAP)
        {
            CCDictionary * dict = new CCDictionary();
            dict->init();
            dictionary_Value(dict, v.asValueMap());
            aArray->addObject(dict);
            dict->release();
        }
        else if (v.getType() == Value::Type::VECTOR)
        {
            CCArray * arr = new CCArray();
            arr->init();
            array_Value(arr, v.asValueVector());
            aArray->addObject(arr);
            arr->release();
        }
        else
        {
            CCString * str = new CCString();
            if (v.getType() == Value::Type::DOUBLE)
                str->initWithFormat("%f", v.asDouble());
            else
                str->initWithFormat("%s", v.asString().c_str());
            aArray->addObject(str);
            str->release();
        }
    }
}
CCObject* CCPointArray::copyWithZone(cocos2d::CCZone *zone)
{
    CCArray *newArray = (CCArray*)m_pControlPoints->copy();
    CCPointArray *points = CCPointArray::create(10);
    points->retain();
    points->setControlPoints(newArray);
    newArray->release();
    
    return points;
}
void CCAnimationCache::parseVersion1(CCDictionary* animations)
{
    CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();

    CCDictElement* pElement = NULL;
    CCDICT_FOREACH(animations, pElement)
    {
        CCDictionary* animationDict = (CCDictionary*)pElement->getObject();
        CCArray* frameNames = (CCArray*)animationDict->objectForKey("frames");
        float delay = animationDict->valueForKey("delay")->floatValue();
        CCAnimation* animation = NULL;

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

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

        CCObject* pObj = NULL;
        CCARRAY_FOREACH(frameNames, pObj)
        {
            const char* frameName = ((CCString*)pObj)->getCString();
            CCSpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName);

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

                continue;
            }

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

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

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

        CCAnimationCache::sharedAnimationCache()->addAnimation(animation, pElement->getStrKey());
        frames->release();
    }    
CCPointArray* CCPointArray::reverse()
{
    CCArray *newArray = new CCArray(m_pControlPoints->count());
    for (int i = m_pControlPoints->count()-1; i >= 0; --i)
    {
        newArray->addObject(m_pControlPoints->objectAtIndex(i));
    }
    CCPointArray *config = CCPointArray::create(0);
    config->setControlPoints(newArray);
    
    newArray->release();
    
    return config;
}
CCAnimation* AnimationManager::createBiqiSmile()
{

	CCSpriteFrame *frame0, *frame1,*frame2;
	CCArray* animFrames =CCArray::createWithCapacity(3);
	frame0=CCSpriteFrame::create("Startup_herface1.png",CCRectMake(0,0,97,92));
	frame1=CCSpriteFrame::create("Startup_herface2.png",CCRectMake(0,0,97,92));
    frame2=CCSpriteFrame::create("Startup_herface3.png",CCRectMake(0,0,97,92));

	animFrames->addObject(frame0);
	animFrames->addObject(frame1);
	animFrames->addObject(frame2);
	CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.6f);
	animFrames->release();

	return animation;
}
CCAnimation* AnimationManager::createVictoryAnimation()
{

	CCSpriteFrame *frame0, *frame1;
	CCArray* animFrames =CCArray::createWithCapacity(2);
	frame0=CCSpriteFrame::create("Navigator_win_image_2.png",CCRectMake(0,0,179,176));
	frame1=CCSpriteFrame::create("Navigator_win_image_1.png",CCRectMake(0,0,179,176));


	animFrames->addObject(frame0);
	animFrames->addObject(frame1);

	CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.1f);
	animFrames->release();

	return animation;
}
Exemple #10
0
void Player::CreateThreeWheelerAnimation()
{
    CCArray *allFrames = new CCArray();
    for (int i = 1 ; i <= 5 ; i++)
    {
        char fn[64];
        sprintf(fn, "wheeler00%d.png" , i );
        allFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn));
    }
    
    CCAnimation *wheelerAnim = CCAnimation::createWithSpriteFrames(allFrames, 0.08f );
    threeWheelerAction = CCRepeatForever::create(CCAnimate::create(wheelerAnim));
    threeWheelerAction->retain();
    
    allFrames->removeAllObjects();
    allFrames->release();
}
Exemple #11
0
bool  Picture::getSyncTempCommandsContent(int maxCount, string &commandsContent, int &commandsCount, bool &hasMore)
{
    int totalCommandCount = mTempCommandList->count();
    Json::Value rootNode;
    hasMore = false;
    commandsCount = 0;
    CCArray *removeArray = CCArray::create();
    for(int i=0;i<totalCommandCount;i++)
    {
        DrawCommand *drawCommand = (DrawCommand *)mTempCommandList->objectAtIndex(i);
        if (drawCommand->mCommandType != DRAW_LINE) {
            continue;
        }
        
        Json::Value item;
        item["id"] = drawCommand->mCommandId;
        item["fromX"] = drawCommand->mFromPoint.x;
        item["fromY"] = drawCommand->mFromPoint.y;
        item["toX"] = drawCommand->mToPoint.x;
        item["toY"] = drawCommand->mToPoint.y;
        item["type"] = drawCommand->mCommandType;
        rootNode.append(item);
        removeArray->addObject(drawCommand);
        if (++commandsCount >= maxCount) {
            if (i <= totalCommandCount - 1) {
                hasMore = true;
            }
            break;
        }
    }
    
    if (commandsCount <= 0) {
        return false;
    }
    
    commandsContent = rootNode.toStyledString();
    mTempCommandList->removeObjectsInArray(removeArray);
    removeArray->release();
    
    return true;
}
void CrazyZombieBomber::PlayMonsterWalkAnimation()
{
    float scaleFactor = CCDirector::sharedDirector()->getContentScaleFactor();

    float x = 32 / scaleFactor;
    float y = 48 / scaleFactor;

    CCSpriteFrame *frame0=CCSpriteFrame::create("CrazyZombieBomber.png",CCRectMake(0, 0, x, y)); 
    CCSpriteFrame *frame1=CCSpriteFrame::create("CrazyZombieBomber.png",CCRectMake(x, 0, x, y)); 
    CCSpriteFrame *frame2=CCSpriteFrame::create("CrazyZombieBomber.png",CCRectMake(x*2, 0, x, y)); 
    CCSpriteFrame *frame3=CCSpriteFrame::create("CrazyZombieBomber.png",CCRectMake(x*3, 0, x, y)); 

    CCArray *animFrames = new CCArray; 
    animFrames->addObject(frame0); 
    animFrames->addObject(frame1); 
    animFrames->addObject(frame2); 
    animFrames->addObject(frame3); 

    CCAnimation *pAnim = CCAnimation::createWithSpriteFrames(animFrames, 0.4f); 
    animFrames->release();

    m_pMainSprite->runAction(CCRepeatForever::create(CCAnimate::create(pAnim)));
}
void getUserResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *userInfo, CCDictionary *error)
{
    if (state == C2DXResponseStateSuccess)
    {
        //输出用户信息
        try
        {
            CCArray *allKeys = userInfo -> allKeys();
			allKeys->retain();
            for (int i = 0; i < allKeys -> count(); i++)
            {
                CCString *key = (CCString *)allKeys -> objectAtIndex(i);
                CCObject *obj = userInfo -> objectForKey(key -> getCString());
                
                CCLog("key = %s", key -> getCString());
                if (dynamic_cast<CCString *>(obj))
                {
                    CCLog("value = %s", dynamic_cast<CCString *>(obj) -> getCString());
                }
                else if (dynamic_cast<CCInteger *>(obj))
                {
                    CCLog("value = %d", dynamic_cast<CCInteger *>(obj) -> getValue());
                }
                else if (dynamic_cast<CCDouble *>(obj))
                {
                    CCLog("value = %f", dynamic_cast<CCDouble *>(obj) -> getValue());
                }
            }
			allKeys->release();
        }
        catch(...)
        {
            CCLog("==============error");
        }
        
    }
}
CCAnimation* AnimationManager::createHeroMovingAnimationByDirection(HeroDirection direction)
{
	CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("Hero_image.png");
	CCSpriteFrame *frame0, *frame1, *frame2;
	CCArray* animFrames ;
	//第二个参数表示显示区域的x, y, width, height,根据direction来确定显示的y坐标
	
	
	frame0 = CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(eSize*0, eSize*direction, eSize, eSize));
	frame1 = CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(eSize*1, eSize*direction, eSize, eSize));

	
	 animFrames = new CCArray(2);
	animFrames->addObject(frame0);
	animFrames->addObject(frame1);
	
	

	CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.09f);

	animFrames->release();
	
	return animation;
}
static void dictionary_Value(CCDictionary * aDict, const cocos2d::ValueMap & value)
{
    cocos2d::ValueMap::const_iterator beg = value.begin();
    cocos2d::ValueMap::const_iterator end = value.end();
    for (; beg != end; ++beg)
    {
        const std::string & key = (*beg).first;
        const cocos2d::Value & v = (*beg).second;
        if (v.getType() == Value::Type::MAP)
        {
            CCDictionary * d = new CCDictionary();
            d->init();
            dictionary_Value(d, v.asValueMap());
            aDict->setObject(d, key);
            d->release();
        }
        else if (v.getType() == Value::Type::VECTOR)
        {
            CCArray * a = new CCArray();
            a->init();
            array_Value(a, v.asValueVector());
            aDict->setObject(a, key);
            a->release();
        }
        else
        {
            CCString * str = new CCString();
            if (v.getType() == Value::Type::DOUBLE)
                str->initWithFormat("%f", v.asDouble());
            else
                str->initWithFormat("%s", v.asString().c_str());
            aDict->setObject(str, key);
            str->release();
        }
    }
}
Exemple #16
0
    void startElement(void *ctx, const char *name, const char **atts)
    {
        CC_UNUSED_PARAM(ctx);
        CC_UNUSED_PARAM(atts);
        std::string sName((char*)name);
        if( sName == "dict" )
        {
            m_pCurDict = new CCDictionary();
            if (m_eResultType == SAX_RESULT_DICT && ! m_pRootDict)
            {
				// Because it will call m_pCurDict->release() later, so retain here.
                m_pRootDict = m_pCurDict;
				m_pRootDict->retain();
            }
            m_tState = SAX_DICT;

            CCSAXState preState = SAX_NONE;
            if (! m_tStateStack.empty())
            {
                preState = m_tStateStack.top();
            }

            if (SAX_ARRAY == preState)
            {
                // add the dictionary into the array
                m_pArray->addObject(m_pCurDict);
            }
            else if (SAX_DICT == preState)
            {
                // add the dictionary into the pre dictionary
                CCAssert(! m_tDictStack.empty(), "The state is wrong!");
                CCDictionary* pPreDict = m_tDictStack.top();
                pPreDict->setObject(m_pCurDict, m_sCurKey);
            }

			m_pCurDict->release();

            // record the dict state
            m_tStateStack.push(m_tState);
            m_tDictStack.push(m_pCurDict);
        }
        else if(sName == "key")
        {
            m_tState = SAX_KEY;
        }
        else if(sName == "integer")
        {
            m_tState = SAX_INT;
        }
        else if(sName == "real")
        {
            m_tState = SAX_REAL;
        }
        else if(sName == "string")
        {
            m_tState = SAX_STRING;
        }
        else if (sName == "array")
        {
            m_tState = SAX_ARRAY;
            m_pArray = new CCArray();
            if (m_eResultType == SAX_RESULT_ARRAY && m_pRootArray == NULL)
            {
                m_pRootArray = m_pArray;
                m_pRootArray->retain();
            }
            CCSAXState preState = SAX_NONE;
            if (! m_tStateStack.empty())
            {
                preState = m_tStateStack.top();
            }

            //CCSAXState preState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top();
            if (preState == SAX_DICT)
            {
                m_pCurDict->setObject(m_pArray, m_sCurKey);
            }
            else if (preState == SAX_ARRAY)
            {
                CCAssert(! m_tArrayStack.empty(), "The state is worng!");
                CCArray* pPreArray = m_tArrayStack.top();
                pPreArray->addObject(m_pArray);
            }
            m_pArray->release();
            // record the array state
            m_tStateStack.push(m_tState);
            m_tArrayStack.push(m_pArray);
        }
        else
        {
            m_tState = SAX_NONE;
        }
    }
SGMonster::SGMonster(int type, int hp, int atk, int inkAmount, int score, const CCPoint* const movePoints,
                     const int nPoints, CCLayer* const parent) : parentLayer(parent) {
    this->maxHP = hp;
    this->nowHP = this->maxHP;
    this->atk = atk;
    
    this->bWakeupMonster = false;
    this->nWakeupFrames = -1;
    this->act_wakeup = NULL;
    
    CCSpriteFrameCache *pSpriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    
    switch (type) {
        case MONSTER_TYPE_MUD: {
            monsterSprite = CCSprite::create(pSpriteFrameCache->spriteFrameByName("mud_wait_1.png"));
            monsterSprite->retain();
            monsterSprite->setPosition(ccp(-500,-500));
            parentLayer->addChild(monsterSprite, ORDER_MONSTER, TAG_TEXTURE);
            
            CCArray* pWalkFrames = CCArray::create();
            CCArray* pWalkActions = CCArray::create();
            for(int i=1; i<=nPoints; i++) {
                pWalkFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_walk_%d.png", i)->getCString()));
            }
            for(int i=0; i<nPoints; i++) {
                pWalkActions->addObject(CCSpawn::create(CCDelayTime::create(GAME_FRAME_SPEED), CCPlace::create(movePoints[i])));
                if(i==11) {
                    //mett or Not
                    pWalkActions->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::confirmBattlePos)));
                }
            }
            pWalkActions->addObject(CCPlace::create(ccp(-500,-500)));
            act_run = CCSpawn::create(CCAnimate::create(CCAnimation::create(pWalkFrames, GAME_FRAME_SPEED)),
                                      CCSequence::create(pWalkActions));
            act_run->retain();
            pWalkActions->release();
            pWalkFrames->release();
            
            CCArray* pWaitFrames = CCArray::create();
            for(int i=1; i<=3; i++) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_wait_%d.png", i)->getCString()));
            }
            pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName("mud_wait_2.png"));
            act_wait = CCRepeatForever::create(CCAnimate::create(CCAnimation::create(pWaitFrames,GAME_FRAME_SPEED)));
            act_wait->retain();
            pWaitFrames->release();
            
            bWakeupMonster = true;
            nWakeupFrames = 6;
            CCArray* pWakeupFrames = CCArray::create();
            for(int i=1; i<=6; i++) {
                pWakeupFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_wakeup_%d.png", i)->getCString()));
            }
            //act_wakeup = CCAnimate::create(CCAnimation::create(pWakeupFrames,GAME_FRAME_SPEED));
            act_wakeup = CCSequence::create(CCAnimate::create(CCAnimation::create(pWakeupFrames,GAME_FRAME_SPEED)),
                                            CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
            act_wakeup->retain();
            pWakeupFrames->release();
            
            CCArray* pDefendFrames = CCArray::create();
            for(int i=1; i<=4; i++) {
                pDefendFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_defend_%d.png", i)->getCString()));
            }
            act_defend = CCSequence::create(CCAnimate::create(CCAnimation::create(pDefendFrames,GAME_FRAME_SPEED)),
                                            CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
            act_defend->retain();
            pDefendFrames->release();
            
            CCArray* pDieFrames = CCArray::create();
            for(int i=1; i<=9; i++) {
                pDieFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_die_%d.png", i)->getCString()));
            }
            act_die = CCSequence::create(CCAnimate::create(CCAnimation::create(pDieFrames,GAME_FRAME_SPEED)),
                                         CCPlace::create(ccp(-500,-500)));
            act_die->retain();
            pDieFrames->release();
            nDieFrames=9;
            
			numAttacks = 1;
            act_attack = new SGAttackAction[numAttacks];
            
            CCArray* pAttackRightFrames = CCArray::create();
            for(int i=1; i<=7; i++) {
                pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_attack_right_%d.png", i)->getCString()));
            }
            for(int i=4; i>=1; i--) {
                pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("mud_attack_right_%d.png", i)->getCString()));
            }
			act_attack[0].atkDir = ATK_DIR_RIGHT;
            act_attack[0].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackRightFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[0].act_attack->retain();
            act_attack[0].nFrames = 7;
            pAttackRightFrames->release();
            
            break;
        }
            
        case MONSTER_TYPE_BAT: {
            monsterSprite = CCSprite::create(pSpriteFrameCache->spriteFrameByName("bat_wait_1.png"));
            monsterSprite->retain();
            monsterSprite->setPosition(ccp(-500,-500));
            parentLayer->addChild(monsterSprite, ORDER_MONSTER, TAG_TEXTURE);
            
            CCArray* pWalkFrames = CCArray::create();
            CCArray* pWalkActions = CCArray::create();
            for(int i=1; i<=nPoints; i++) {
                pWalkFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_walk_%d.png", i)->getCString()));
            }
            for(int i=0; i<nPoints; i++) {
                pWalkActions->addObject(CCSpawn::create(CCDelayTime::create(GAME_FRAME_SPEED), CCPlace::create(movePoints[i])));
                if(i==11) {
                    //mett or Not
                    pWalkActions->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::confirmBattlePos)));
                }
            }
            pWalkActions->addObject(CCPlace::create(ccp(-500,-500)));
            act_run = CCSpawn::create(CCAnimate::create(CCAnimation::create(pWalkFrames, GAME_FRAME_SPEED)),
                                      CCSequence::create(pWalkActions));
            act_run->retain();
            pWalkActions->release();
            pWalkFrames->release();
            
            CCArray* pWaitFrames = CCArray::create();
            for(int i=1; i<=7; i++) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_wait_%d.png", i)->getCString()));
            }
            for(int i=6; i>=2; i--) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_wait_%d.png", i)->getCString()));
            }
            act_wait = CCRepeatForever::create(CCAnimate::create(CCAnimation::create(pWaitFrames,GAME_FRAME_SPEED)));
            act_wait->retain();
            pWaitFrames->release();
            
            CCArray* pDefendFrames = CCArray::create();
            for(int i=1; i<=3; i++) {
                pDefendFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_defend_%d.png", i)->getCString()));
            }
            act_defend = CCSequence::create(CCAnimate::create(CCAnimation::create(pDefendFrames,GAME_FRAME_SPEED)),
                                            CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
            act_defend->retain();
            pDefendFrames->release();
            
            CCArray* pDieFrames = CCArray::create();
            for(int i=1; i<=9; i++) {
                pDieFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_die_%d.png", i)->getCString()));
            }
            act_die = CCSequence::create(CCAnimate::create(CCAnimation::create(pDieFrames,GAME_FRAME_SPEED)),
                                         CCPlace::create(ccp(-500,-500)));
            act_die->retain();
            pDieFrames->release();
            nDieFrames=9;
            
			numAttacks = 2;
            act_attack = new SGAttackAction[numAttacks];
            
            CCArray* pAttackLeftFrames = CCArray::create();
            for(int i=1; i<=11; i++) {
                pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_attack_left_%d.png", i)->getCString()));
            }
			pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_wait_2.png")->getCString()));
			act_attack[0].atkDir = ATK_DIR_LEFT;
			act_attack[0].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackLeftFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[0].act_attack->retain();
            act_attack[0].nFrames = 9;
            pAttackLeftFrames->release();
            
            CCArray* pAttackRightFrames = CCArray::create();
            for(int i=1; i<=11; i++) {
                pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_attack_right_%d.png", i)->getCString()));
            }
			pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("bat_wait_2.png")->getCString()));
			act_attack[1].atkDir = ATK_DIR_RIGHT;
            act_attack[1].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackRightFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[1].act_attack->retain();
            act_attack[1].nFrames = 9;
            pAttackRightFrames->release();
            
            break;
        }
            
        case MONSTER_TYPE_WING: {
            monsterSprite = CCSprite::create(pSpriteFrameCache->spriteFrameByName("wing_wait_1.png"));
            monsterSprite->retain();
            monsterSprite->setPosition(ccp(-500,-500));
            parentLayer->addChild(monsterSprite, ORDER_MONSTER, TAG_TEXTURE);
            
            CCArray* pWalkFrames = CCArray::create();
            CCArray* pWalkActions = CCArray::create();
            for(int i=1; i<=nPoints; i++) {
                pWalkFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_walk_%d.png", i)->getCString()));
            }
            for(int i=0; i<nPoints; i++) {
                pWalkActions->addObject(CCSpawn::create(CCDelayTime::create(GAME_FRAME_SPEED), CCPlace::create(movePoints[i])));
                if(i==11) {
                    //mett or Not
                    pWalkActions->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::confirmBattlePos)));
                }
            }
            pWalkActions->addObject(CCPlace::create(ccp(-500,-500)));
            act_run = CCSpawn::create(CCAnimate::create(CCAnimation::create(pWalkFrames, GAME_FRAME_SPEED)),
                                      CCSequence::create(pWalkActions));
            act_run->retain();
            pWalkActions->release();
            pWalkFrames->release();
            
            CCArray* pWaitFrames = CCArray::create();
            for(int i=1; i<=5; i++) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_wait_%d.png", i)->getCString()));
            }
            for(int i=4; i>=2; i--) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_wait_%d.png", i)->getCString()));
            }
            act_wait = CCRepeatForever::create(CCAnimate::create(CCAnimation::create(pWaitFrames,GAME_FRAME_SPEED)));
            act_wait->retain();
            pWaitFrames->release();
            
            CCArray* pDefendFrames = CCArray::create();
            for(int i=1; i<=3; i++) {
                pDefendFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_defend_%d.png", i)->getCString()));
            }
            act_defend = CCSequence::create(CCAnimate::create(CCAnimation::create(pDefendFrames,GAME_FRAME_SPEED)),
                                            CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
            act_defend->retain();
            pDefendFrames->release();
            
            CCArray* pDieFrames = CCArray::create();
            for(int i=1; i<=8; i++) {
                pDieFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_die_%d.png", i)->getCString()));
            }
            act_die = CCSequence::create(CCAnimate::create(CCAnimation::create(pDieFrames,GAME_FRAME_SPEED)),
                                         CCPlace::create(ccp(-500,-500)));
            act_die->retain();
            pDieFrames->release();
            nDieFrames=8;
            
			numAttacks = 3;
            act_attack = new SGAttackAction[numAttacks];
            
            CCArray* pAttackLeftFrames = CCArray::create();
            pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName("wing_attack_1.png"));
            for(int i=2; i<=5; i++) {
                pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_attack_left_%d.png", i)->getCString()));
            }
			act_attack[0].atkDir = ATK_DIR_LEFT;
			act_attack[0].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackLeftFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[0].act_attack->retain();
            act_attack[0].nFrames = 5;
            pAttackLeftFrames->release();
            
            CCArray* pAttackRightFrames = CCArray::create();
            pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName("wing_attack_1.png"));
            for(int i=2; i<=5; i++) {
                pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_attack_right_%d.png", i)->getCString()));
            }
			act_attack[1].atkDir = ATK_DIR_RIGHT;
            act_attack[1].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackRightFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[1].act_attack->retain();
            act_attack[1].nFrames = 5;
            pAttackRightFrames->release();
            
            CCArray* pAttackUpFrames = CCArray::create();
            pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName("wing_attack_up_.png"));
            for(int i=3; i<=7; i++) {
                pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("wing_attack_up_%d.png", i)->getCString()));
            }
            pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName("wing_attack_up_4.png"));
            pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName("wing_attack_up_3.png"));
			act_attack[2].atkDir = ATK_DIR_UP;
            act_attack[2].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackUpFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[2].act_attack->retain();
            act_attack[2].nFrames = 5;
            pAttackUpFrames->release();
            
            break;
        }
            
        case MONSTER_TYPE_BALL: {
            monsterSprite = CCSprite::create(pSpriteFrameCache->spriteFrameByName("ball_wait_1.png"));
            monsterSprite->retain();
            monsterSprite->setPosition(ccp(-500,-500));
            parentLayer->addChild(monsterSprite, ORDER_MONSTER, TAG_TEXTURE);
            
            CCArray* pWalkFrames = CCArray::create();
            CCArray* pWalkActions = CCArray::create();
            for(int i=1; i<=nPoints; i++) {
                pWalkFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_walk_%d.png", i)->getCString()));
            }
            for(int i=0; i<nPoints; i++) {
                pWalkActions->addObject(CCSpawn::create(CCDelayTime::create(GAME_FRAME_SPEED), CCPlace::create(movePoints[i])));
                if(i==11) {
                    //mett or Not
                    pWalkActions->addObject(CCCallFunc::create(this, callfunc_selector(SGMonster::confirmBattlePos)));
                }
            }
            pWalkActions->addObject(CCPlace::create(ccp(-500,-500)));
            act_run = CCSpawn::create(CCAnimate::create(CCAnimation::create(pWalkFrames, GAME_FRAME_SPEED)),
                                      CCSequence::create(pWalkActions));
            act_run->retain();
            pWalkActions->release();
            pWalkFrames->release();
            
            CCArray* pWaitFrames = CCArray::create();
            for(int i=1; i<=3; i++) {
                pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_wait_%d.png", i)->getCString()));
            }
            pWaitFrames->addObject(pSpriteFrameCache->spriteFrameByName("ball_wait_2.png"));
            act_wait = CCRepeatForever::create(CCAnimate::create(CCAnimation::create(pWaitFrames, GAME_FRAME_SPEED)));
            act_wait->retain();
            pWaitFrames->release();
            
            CCArray* pDefendFrames = CCArray::create();
            for(int i=1; i<=2; i++) {
                pDefendFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_defend_%d.png", i)->getCString()));
            }
            act_defend = CCSequence::create(CCAnimate::create(CCAnimation::create(pDefendFrames,GAME_FRAME_SPEED)),
                                            CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
            act_defend->retain();
            pDefendFrames->release();
            
            CCArray* pDieFrames = CCArray::create();
            for(int i=1; i<=8; i++) {
                pDieFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_die_%d.png", i)->getCString()));
            }
            act_die = CCSequence::create(CCAnimate::create(CCAnimation::create(pDieFrames,GAME_FRAME_SPEED)),
                                         CCPlace::create(ccp(-500,-500)));
            act_die->retain();
            pDieFrames->release();
            nDieFrames=8;
            
			numAttacks = 4;
            act_attack = new SGAttackAction[numAttacks];
            
            CCArray* pAttackLeftFrames = CCArray::create();
            for(int i=1; i<=7; i++) {
                pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_attack_left_%d.png", i)->getCString()));
            }
			pAttackLeftFrames->addObject(pSpriteFrameCache->spriteFrameByName("ball_attack_right_1.png"));
			act_attack[0].atkDir = ATK_DIR_LEFT;
			act_attack[0].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackLeftFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[0].act_attack->retain();
            act_attack[0].nFrames = 7;
            pAttackLeftFrames->release();
            
            CCArray* pAttackRightFrames = CCArray::create();
            for(int i=1; i<=7; i++) {
                pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_attack_right_%d.png", i)->getCString()));
            }
			pAttackRightFrames->addObject(pSpriteFrameCache->spriteFrameByName("ball_attack_left_1.png"));
			act_attack[1].atkDir = ATK_DIR_RIGHT;
            act_attack[1].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackRightFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[1].act_attack->retain();
            act_attack[1].nFrames = 7;
            pAttackRightFrames->release();
            
            CCArray* pAttackUpFrames = CCArray::create();
            for(int i=1; i<=5; i++) {
                pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_attack_up_%d.png", i)->getCString()));
            }
            pAttackUpFrames->addObject(pSpriteFrameCache->spriteFrameByName("ball_attack_up_2.png"));
			act_attack[2].atkDir = ATK_DIR_UP;
            act_attack[2].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackUpFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[2].act_attack->retain();
            act_attack[2].nFrames = 5;
            pAttackUpFrames->release();
            
            CCArray* pAttackDownFrames = CCArray::create();
            for(int i=1; i<=5; i++) {
                pAttackDownFrames->addObject(pSpriteFrameCache->spriteFrameByName(CCString::createWithFormat("ball_attack_down_%d.png", i)->getCString()));
            }
			act_attack[3].atkDir = ATK_DIR_DOWN;
            act_attack[3].act_attack = CCSequence::create(CCAnimate::create(CCAnimation::create(pAttackDownFrames,GAME_FRAME_SPEED)),
                                                          CCCallFunc::create(this, callfunc_selector(SGMonster::func_waiting)));
			act_attack[3].act_attack->retain();
            act_attack[3].nFrames = 4;
            pAttackDownFrames->release();
            break;
        }
    }
}
// the XML parser calls here with all the elements
void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
{    
    CC_UNUSED_PARAM(ctx);
    CCTMXMapInfo *pTMXMapInfo = this;
    std::string elementName = (char*)name;
    std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
    if (atts && atts[0])
    {
        for(int i = 0; atts[i]; i += 2) 
        {
            std::string key = (char*)atts[i];
            std::string value = (char*)atts[i+1];
            attributeDict->insert(pair<std::string, std::string>(key, value));
        }
    }
    if (elementName == "map")
    {
        std::string version = valueForKey("version", attributeDict);
        if ( version != "1.0")
        {
            CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %s", version.c_str());
        }
        std::string orientationStr = valueForKey("orientation", attributeDict);
        if (orientationStr == "orthogonal")
            pTMXMapInfo->setOrientation(CCTMXOrientationOrtho);
        else if (orientationStr  == "isometric")
            pTMXMapInfo->setOrientation(CCTMXOrientationIso);
        else if(orientationStr == "hexagonal")
            pTMXMapInfo->setOrientation(CCTMXOrientationHex);
        else
            CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation());

        CCSize s;
        s.width = (float)atof(valueForKey("width", attributeDict));
        s.height = (float)atof(valueForKey("height", attributeDict));
        pTMXMapInfo->setMapSize(s);

        s.width = (float)atof(valueForKey("tilewidth", attributeDict));
        s.height = (float)atof(valueForKey("tileheight", attributeDict));
        pTMXMapInfo->setTileSize(s);

        // The parent element is now "map"
        pTMXMapInfo->setParentElement(TMXPropertyMap);
    } 
    else if (elementName == "tileset") 
    {
        // If this is an external tileset then start parsing that
        std::string externalTilesetFilename = valueForKey("source", attributeDict);
        if (externalTilesetFilename != "")
        {
            // Tileset file will be relative to the map file. So we need to convert it to an absolute path
            if (m_sTMXFileName.find_last_of("/") != string::npos)
            {
                string dir = m_sTMXFileName.substr(0, m_sTMXFileName.find_last_of("/") + 1);
                externalTilesetFilename = dir + externalTilesetFilename;
            }
            else 
            {
                externalTilesetFilename = m_sResources + "/" + externalTilesetFilename;
            }
            externalTilesetFilename = CCFileUtils::sharedFileUtils()->fullPathForFilename(externalTilesetFilename.c_str());
            
            m_uCurrentFirstGID = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
            
            pTMXMapInfo->parseXMLFile(externalTilesetFilename.c_str());
        }
        else
        {
            CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo();
            tileset->m_sName = valueForKey("name", attributeDict);
            if (m_uCurrentFirstGID == 0)
            {
                tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
            }
            else
            {
                tileset->m_uFirstGid = m_uCurrentFirstGID;
                m_uCurrentFirstGID = 0;
            }
            tileset->m_uSpacing = (unsigned int)atoi(valueForKey("spacing", attributeDict));
            tileset->m_uMargin = (unsigned int)atoi(valueForKey("margin", attributeDict));
            CCSize s;
            s.width = (float)atof(valueForKey("tilewidth", attributeDict));
            s.height = (float)atof(valueForKey("tileheight", attributeDict));
            tileset->m_tTileSize = s;

            pTMXMapInfo->getTilesets()->addObject(tileset);
            tileset->release();
        }
    }
    else if (elementName == "tile")
    {
        CCTMXTilesetInfo* info = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
        CCDictionary *dict = new CCDictionary();
        pTMXMapInfo->setParentGID(info->m_uFirstGid + atoi(valueForKey("id", attributeDict)));
        pTMXMapInfo->getTileProperties()->setObject(dict, pTMXMapInfo->getParentGID());
        CC_SAFE_RELEASE(dict);
        
        pTMXMapInfo->setParentElement(TMXPropertyTile);

    }
    else if (elementName == "layer")
    {
        CCTMXLayerInfo *layer = new CCTMXLayerInfo();
        layer->m_sName = valueForKey("name", attributeDict);

        CCSize s;
        s.width = (float)atof(valueForKey("width", attributeDict));
        s.height = (float)atof(valueForKey("height", attributeDict));
        layer->m_tLayerSize = s;

        std::string visible = valueForKey("visible", attributeDict);
        layer->m_bVisible = !(visible == "0");

        std::string opacity = valueForKey("opacity", attributeDict);
        if( opacity != "" )
        {
            layer->m_cOpacity = (unsigned char)(255 * atof(opacity.c_str()));
        }
        else
        {
            layer->m_cOpacity = 255;
        }

        float x = (float)atof(valueForKey("x", attributeDict));
        float y = (float)atof(valueForKey("y", attributeDict));
        layer->m_tOffset = ccp(x,y);

        pTMXMapInfo->getLayers()->addObject(layer);
        layer->release();

        // The parent element is now "layer"
        pTMXMapInfo->setParentElement(TMXPropertyLayer);

    } 
    else if (elementName == "objectgroup")
    {
        CCTMXObjectGroup *objectGroup = new CCTMXObjectGroup();
        objectGroup->setGroupName(valueForKey("name", attributeDict));
        CCPoint positionOffset;
        positionOffset.x = (float)atof(valueForKey("x", attributeDict)) * pTMXMapInfo->getTileSize().width;
        positionOffset.y = (float)atof(valueForKey("y", attributeDict)) * pTMXMapInfo->getTileSize().height;
        objectGroup->setPositionOffset(positionOffset);

        pTMXMapInfo->getObjectGroups()->addObject(objectGroup);
        objectGroup->release();

        // The parent element is now "objectgroup"
        pTMXMapInfo->setParentElement(TMXPropertyObjectGroup);

    }
    else if (elementName == "image")
    {
        CCTMXTilesetInfo* tileset = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();

        // build full path
        std::string imagename = valueForKey("source", attributeDict);

        if (m_sTMXFileName.find_last_of("/") != string::npos)
        {
            string dir = m_sTMXFileName.substr(0, m_sTMXFileName.find_last_of("/") + 1);
            tileset->m_sSourceImage = dir + imagename;
        }
        else 
        {
            tileset->m_sSourceImage = m_sResources + (m_sResources.size() ? "/" : "") + imagename;
        }
    } 
    else if (elementName == "data")
    {
        std::string encoding = valueForKey("encoding", attributeDict);
        std::string compression = valueForKey("compression", attributeDict);

        if( encoding == "base64" )
        {
            int layerAttribs = pTMXMapInfo->getLayerAttribs();
            pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribBase64);
            pTMXMapInfo->setStoringCharacters(true);

            if( compression == "gzip" )
            {
                layerAttribs = pTMXMapInfo->getLayerAttribs();
                pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribGzip);
            } else
            if (compression == "zlib")
            {
                layerAttribs = pTMXMapInfo->getLayerAttribs();
                pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib);
            }
            CCAssert( compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method" );
        }
        CCAssert( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported" );

    } 
    else if (elementName == "object")
    {
        char buffer[32] = {0};
        CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();

        // The value for "type" was blank or not a valid class name
        // Create an instance of TMXObjectInfo to store the object and its properties
        CCDictionary *dict = new CCDictionary();
        // Parse everything automatically
        const char* pArray[] = {"name", "type", "width", "height", "gid"};
        
        for(size_t i = 0; i < sizeof(pArray)/sizeof(pArray[0]); ++i )
        {
            const char* key = pArray[i];
            CCString* obj = new CCString(valueForKey(key, attributeDict));
            if( obj )
            {
                obj->autorelease();
                dict->setObject(obj, key);
            }
        }

        // But X and Y since they need special treatment
        // X

        const char* value = valueForKey("x", attributeDict);
        if (value) 
        {
            int x = atoi(value) + (int)objectGroup->getPositionOffset().x;
            sprintf(buffer, "%d", x);
            CCString* pStr = new CCString(buffer);
            pStr->autorelease();
            dict->setObject(pStr, "x");
        }

        // Y
        value = valueForKey("y", attributeDict);
        if (value)  {
            int y = atoi(value) + (int)objectGroup->getPositionOffset().y;

            // Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
            y = (int)(m_tMapSize.height * m_tTileSize.height) - y - atoi(valueForKey("height", attributeDict));
            sprintf(buffer, "%d", y);
            CCString* pStr = new CCString(buffer);
            pStr->autorelease();
            dict->setObject(pStr, "y");
        }

        // Add the object to the objectGroup
        objectGroup->getObjects()->addObject(dict);
        dict->release();

         // The parent element is now "object"
         pTMXMapInfo->setParentElement(TMXPropertyObject);

    }
    else if (elementName == "property")
    {
        if ( pTMXMapInfo->getParentElement() == TMXPropertyNone ) 
        {
            CCLOG( "TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'",
                valueForKey("name", attributeDict), valueForKey("value",attributeDict) );
        } 
        else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap )
        {
            // The parent element is the map
            CCString *value = new CCString(valueForKey("value", attributeDict));
            std::string key = valueForKey("name", attributeDict);
            pTMXMapInfo->getProperties()->setObject(value, key.c_str());
            value->release();

        } 
        else if ( pTMXMapInfo->getParentElement() == TMXPropertyLayer )
        {
            // The parent element is the last layer
            CCTMXLayerInfo* layer = (CCTMXLayerInfo*)pTMXMapInfo->getLayers()->lastObject();
            CCString *value = new CCString(valueForKey("value", attributeDict));
            std::string key = valueForKey("name", attributeDict);
            // Add the property to the layer
            layer->getProperties()->setObject(value, key.c_str());
            value->release();

        } 
        else if ( pTMXMapInfo->getParentElement() == TMXPropertyObjectGroup ) 
        {
            // The parent element is the last object group
            CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
            CCString *value = new CCString(valueForKey("value", attributeDict));
            const char* key = valueForKey("name", attributeDict);
            objectGroup->getProperties()->setObject(value, key);
            value->release();

        } 
        else if ( pTMXMapInfo->getParentElement() == TMXPropertyObject )
        {
            // The parent element is the last object
            CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
            CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();

            const char* propertyName = valueForKey("name", attributeDict);
            CCString *propertyValue = new CCString(valueForKey("value", attributeDict));
            dict->setObject(propertyValue, propertyName);
            propertyValue->release();
        } 
        else if ( pTMXMapInfo->getParentElement() == TMXPropertyTile ) 
        {
            CCDictionary* dict = (CCDictionary*)pTMXMapInfo->getTileProperties()->objectForKey(pTMXMapInfo->getParentGID());

            const char* propertyName = valueForKey("name", attributeDict);
            CCString *propertyValue = new CCString(valueForKey("value", attributeDict));
            dict->setObject(propertyValue, propertyName);
            propertyValue->release();
        }
    }
    else if (elementName == "polygon") 
    {
        // find parent object's dict and add polygon-points to it
        CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject();
        CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();

        // get points value string
        const char* value = valueForKey("points", attributeDict);
        if(value)
        {
            CCArray* pPointsArray = new CCArray;

            // parse points string into a space-separated set of points
            stringstream pointsStream(value);
            string pointPair;
            while(std::getline(pointsStream, pointPair, ' '))
            {
                // parse each point combo into a comma-separated x,y point
                stringstream pointStream(pointPair);
                string xStr,yStr;
                char buffer[32] = {0};
                
                CCDictionary* pPointDict = new CCDictionary;

                // set x
                if(std::getline(pointStream, xStr, ','))
                {
                    int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
                    sprintf(buffer, "%d", x);
                    CCString* pStr = new CCString(buffer);
                    pStr->autorelease();
                    pPointDict->setObject(pStr, "x");
                }

                // set y
                if(std::getline(pointStream, yStr, ','))
                {
                    int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
                    sprintf(buffer, "%d", y);
                    CCString* pStr = new CCString(buffer);
                    pStr->autorelease();
                    pPointDict->setObject(pStr, "y");
                }
                
                // add to points array
                pPointsArray->addObject(pPointDict);
                pPointDict->release();
            }
            
            dict->setObject(pPointsArray, "points");
            pPointsArray->release();
        }
    }
    else if (elementName == "polyline")
    {
        // find parent object's dict and add polyline-points to it
        // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject();
        // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();
        // TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"];
		CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject();
		CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();

		// get points value string
		const char* value = valueForKey("points", attributeDict);
		if(value)
		{
			CCArray* pPointsArray = new CCArray;

			// parse points string into a space-separated set of points
			stringstream pointsStream(value);
			string pointPair;
			while(std::getline(pointsStream, pointPair, ' '))
			{
				// parse each point combo into a comma-separated x,y point
				stringstream pointStream(pointPair);
				string xStr,yStr;
				char buffer[32] = {0};

				CCDictionary* pPointDict = new CCDictionary;

				// set x
				if(std::getline(pointStream, xStr, ','))
				{
					int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
					sprintf(buffer, "%d", x);
					CCString* pStr = new CCString(buffer);
					pStr->autorelease();
					pPointDict->setObject(pStr, "x");
				}

				// set y
				if(std::getline(pointStream, yStr, ','))
				{
					int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
					sprintf(buffer, "%d", y);
					CCString* pStr = new CCString(buffer);
					pStr->autorelease();
					pPointDict->setObject(pStr, "y");
				}

				// add to points array
				pPointsArray->addObject(pPointDict);
				pPointDict->release();
			}

			dict->setObject(pPointsArray, "points");
			pPointsArray->release();
		}
    }

    if (attributeDict)
    {
        attributeDict->clear();
        delete attributeDict;
    }
}
void HelloWorld::update(float dt) {
    
    
    DataModel *m =DataModel:: getModel();
    CCArray *projectilesToDelete =new CCArray();
    
    Bullet *projectile =NULL;
	/*CCLog("projectileRect count %d",  m->getProjectiles()->count());*/
    for (int i=0;i<m->getProjectiles()->count();i++) {
        projectile=(Bullet *)m->getProjectiles()->objectAtIndex(i);
        
        CCRect projectileRect = CCRectMake(projectile->getPosition().x - (projectile->getContentSize().width/2),
                                           projectile->getPosition().y - (projectile->getContentSize().height/2),
                                           projectile->getContentSize().width,
                                           projectile->getContentSize().height);
       /* CCLog("projectileRect width %f height %f ",  projectile->getContentSize().width,  projectile->getContentSize().height);*/
        CCArray *targetsToDelete = new CCArray();
        for (int j=0;j<m->getTargets()->count();j++) {
            CCSprite *target=(CCSprite *)m->getTargets()->objectAtIndex(j);
            CCRect targetRect = CCRectMake(target->getPosition().x - (target->getContentSize().width/2),
                                           target->getPosition().y - (target->getContentSize().height/2),
                                           target->getContentSize().width,
                                           target->getContentSize().height);
            
            //                if (CGRectIntersectsRect(projectileRect, targetRect)) {
            if(projectileRect.intersectsRect(targetRect)){
                projectilesToDelete->addObject(projectile);
                
                 Creeps *creep = (Creeps *)target;
                int bulletType=projectile->getTag();
				/*CCLog("hellocpp update bullettype %d",bulletType);*/
                if (bulletType ==0){//MachineGun Projectile
                   
                    int hp=creep->getHp();
                    creep->setHp(--hp);
                    //               
                    if (creep->getHp() <= 0) {
                        targetsToDelete->addObject(target);
                        gameHUD->updateResources(1);
                         removeChild(creep->getHealthBar(),true);
                    }
                    break;
                }
                else if (bulletType ==1){//Freeze projectile
                    CCMoveTo*actionFreeze = CCMoveTo::create(1,creep->getPosition());
                    CCCallFuncN* actionMoveResume = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::ResumePath));
                    creep->stopAllActions();
                    creep->runAction(CCSequence:: create(actionFreeze, actionMoveResume, NULL));
                    break;
                }
                break;
                
            }
        }
        for (int k=0; k<targetsToDelete->count(); k++) {
            CCSprite* target=(CCSprite*)targetsToDelete->objectAtIndex(k);
            m->getTargets()->removeObject(target);
            removeChild(target, true);
        }
        targetsToDelete->release();
    }
    
    for (int k=0; k<projectilesToDelete->count(); k++) {
        CCSprite *projectile=(CCSprite *)projectilesToDelete->objectAtIndex(k);
        m->getProjectiles()->removeObject(projectile);
        removeChild(projectile,true);
    }
    projectilesToDelete->release();
    
    Wave *wave =getCurrentWave();
    
    if (wave!=NULL&&m->getTargets()->count() ==0 && wave->getRedCreeps()<=0&&wave->getGreenCreeps()<=0){
        
//        getNextWave();
//         gameHUD->updateWaveCount();
        schedule(schedule_selector(HelloWorld::waveWait),3.0);
         gameHUD->newWaveApproaching();
        }
    
}
 /// Callback function used by libcurl for collect header data
 static size_t writeHeaderData(void* ptr, size_t size, size_t nmemb, void* userdata) {
     CURLHandler* handler = (CURLHandler*)userdata;
     size_t sizes = size * nmemb;
     
     // lock
     pthread_mutex_lock(&handler->m_mutex);
     
     // parse pair
     string header((const char*)ptr, sizes);
     CCArray* pair = new CCArray();
     if(!header.empty()) {
         // remove head and tailing brace, bracket, parentheses
         size_t start = 0;
         size_t end = header.length() - 1;
         char c = header[start];
         while(c == '{' || c == '[' || c == '(') {
             start++;
             c = header[start];
         }
         c = header[end];
         while(c == '}' || c == ']' || c == ')') {
             end--;
             c = header[end];
         }
         
         // iterate string
         size_t compStart = start;
         for(size_t i = start; i <= end; i++) {
             c = header[i];
             if(c == ':') {
                 CCString* s = new CCString(header.substr(compStart, i - compStart));
                 pair->addObject(s);
                 s->release();
                 compStart = i + 1;
             } else if(c == ' ' || c == '\t' || c == '\r' || c == '\n') {
                 if(compStart == i) {
                     compStart++;
                 }
             }
         }
         
         // last comp
         // or, if last char is separator, append an empty string
         if(compStart <= end) {
             CCString* s = new CCString(header.substr(compStart, end - compStart + 1));
             pair->addObject(s);
             s->release();
         } else if(header[end] == ':') {
             CCString* s = new CCString("");
             pair->addObject(s);
             s->release();
         }
     }
     
     // if pair count is two, means ok
     if(pair->count() == 2) {
         handler->m_ctx->response->addHeader(((CCString*)pair->objectAtIndex(0))->getCString(),
                                             ((CCString*)pair->objectAtIndex(1))->getCString());
     }
     
     // release array
     pair->release();
     
     // unlock
     pthread_mutex_unlock(&handler->m_mutex);
     
     // return a value which is different with sizes will abort it
     if(handler->m_ctx->request->isCancel())
         return sizes + 1;
     else
         return sizes;
 }
void ScheduleManager::finishScheduleFunction(CCNode* sender, void* data)
{
    CCArray* prms = static_cast<CCArray*>(data);
    prms->release();
}