Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my game there are certain zombies coming from top of the screen.I have stored all zombies sprites in an CCArray.Then using foreach loop I am making them falling down. I just want to perform combo.It means that whenever I kill a zombie on tap, the combo_counter increases. On killing two consecutive zombies the combo_counter goes to 2 but if I tap at any other location on the screen the combo_counter should go to 0.

So my problem is how to detect whether I have not tapped a zombie and tapped anyother place on the screen.I am attaching my code also of cctouchbegan method

zombies is a CCArray where all zombie sprites are stored

C++
void Level1::ccTouchesBegan(cocos2d::CCSet *pTouch, cocos2d::CCEvent *pEvent)
{

CCTouch* touch = (CCTouch*)(pTouch->anyObject());
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);

CCObject* touchedzombie;

CCARRAY_FOREACH(zombies, touchedzombie)
{


    if(!((CCSprite*) touchedzombie)->isVisible())
        continue;
    //

    if(((CCSprite*)touchedzombie)==zombies->objectAtIndex(0))
    {

        //   if((CCSprite*(touchedzombie)==zombies-))
        if(touchedzombie!=NULL&&((CCSprite*)touchedzombie)->boundingBox().containsPoint(location))
        {

            this->setScoreonGame();
            combo_counter++;
            CCString *comboString=CCString::createWithFormat("comboX %d",combo_counter);

            zombies_left--;
            CCLOG("left = %d",zombies_left);
            CCSize tt=((CCSprite*)touchedzombie)->getContentSize();
            CCPoint pos_of_sprite=((CCSprite*)touchedzombie)->getPosition();
            int rand_die1=Level1::random1();
            CCString *str = CCString::createWithFormat("z2%d.png", rand_die1);
            changedSprite = CCSprite::create(str->getCString());
            CCLOG("Inside index 0");

            ((CCSprite*)touchedzombie)->setVisible(false);

            changedSprite->setPositionX(pos_of_sprite.x);
            changedSprite->setPositionY(pos_of_sprite.y);
            changedSprite->setScaleX(Utils::getScaleX());
            changedSprite->setScaleY(Utils::getScaleY());
            this->addChild(changedSprite);


            combo=CCLabelTTF::create(comboString->getCString(), "HoboStd", 50);
            combo->setColor(ccRED);
            combo->setPosition((ccp(changedSprite->getContentSize().width*0.50,changedSprite->getContentSize().height*1.05)));
            changedSprite->addChild(combo,40);



            this->runAction(CCSequence::create(delayAction,
                                               callSelectorAction,
                                               NULL));


            this->removeChild( ((CCSprite*)touchedzombie),true);
            this->Level1::reloadZombies();
            //  touchedzombie=NULL;

        }






    }





    if(((CCSprite*)touchedzombie)==zombies->objectAtIndex(3))
    {


        //   if((CCSprite*(touchedzombie)==zombies-))
        if(touchedzombie!=NULL&&((CCSprite*)touchedzombie)->boundingBox().containsPoint(location))
        {
         //    iftouched++;
            this->setScoreonGame();
            combo_counter++;
            CCString *comboString=CCString::createWithFormat("comboX %d",combo_counter);

            zombies_left--;
            CCLOG("left = %d",zombies_left);
            CCSize tt=((CCSprite*)touchedzombie)->getContentSize();
            CCPoint pos_of_sprite=((CCSprite*)touchedzombie)->getPosition();
            int rand_die1=Level1::random1();
            CCString *str = CCString::createWithFormat("z2%d.png", rand_die1);
            changedSprite3 = CCSprite::create(str->getCString());
            //  CCLOG("%s",str->getCString());


            //  CCLOG("Sprite Toucheddd");
            CCLOG("Inside index 4");
            // CCLog("width= %f  height =%f",tt.width,tt.height);
            // CCLog("x location =%f  y location =%f",location.x,location.y);
            //  CCLog("Positon of Sprite X=%f  Y=%f",pos_of_sprite.x,pos_of_sprite.y);

            ((CCSprite*)touchedzombie)->setVisible(false);

            changedSprite3->setPositionX(pos_of_sprite.x);
            changedSprite3->setPositionY(pos_of_sprite.y);
            changedSprite3->setScaleX(Utils::getScaleX());
            changedSprite3->setScaleY(Utils::getScaleY());
            this->addChild(changedSprite3);

            combo=CCLabelTTF::create(comboString->getCString(), "HoboStd", 50);
            combo->setColor(ccRED);
            combo->setPosition((ccp(changedSprite3->getContentSize().width*0.50,changedSprite3->getContentSize().height*1.05)));
            changedSprite3->addChild(combo,40);


            this->runAction(CCSequence::create(delayAction,
                                               callSelectorAction3,
                                               NULL));

            this->removeChild( ((CCSprite*)touchedzombie),true);
            this->Level1::reloadZombies();
            touchedzombie=NULL;
        }
        //..upto 9 indexes...      
    }
}
Posted

This is a late response - so if it's solved already just tell me to shut up.

I can't make sense of what you are trying to do in your code at all - but what I would do:

IN the touches began, store the location of the touch in some property somewhere (maybe in the layer?) and do not more processing in the touchesbagen method.

Somewhere (maybe in the Layer->update() method):

C++
int index = 0;

bool hitZombie= false;
while (index < zombiearray.length() && !finished)
{
   zombie = zombieArray[index];
   if (zombie.x < touch.x && zombie.x + zombie.width > touch.x
      && zombie.y < touch.y && zombie.y + zombie.height > touch.y)
   {
      handleZombieBeingTouched(zombie);
      hitZombie= true;
   }
  index ++
}

if (!hitZombie)
  {
    handleNoZombieHit();
  }



There's you really need to do - like storing touch as an array so lots of fast touches are handled,
but the above would probably give you the basics
 
Share this answer
 
pseudo code

if( zombie1 )
{
....
return
}

if( zombie2 )
{
....
return
}

//OMG it isnt a zombie :-O
tapped_some_else = true;
 
Share this answer
 
Comments
Rajan Maheshwari 11-Feb-14 11:10am    
its not as simple as you wrote the pseudo....i have tried many things then only i posted on codeproject..i dnt have to return and exit the foreach loop...this loop will keep on looping forever
KarstenK 12-Feb-14 2:14am    
"looping forever" doesnt sound so nice.

Isnt ccTouchesBegan the event handler for a touch. So the event handling should end if done?

What is the difference between zombie1 and zombie2? I would write a function like

UpdateZombie( Zombie1 )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900