如何使用c ++在cocos2d-x ios游戏中检测除了顶部坠落物体之外的触摸
在我的游戏中,某些僵尸来自屏幕顶部。我已将所有僵尸精灵存储在CCArray中。然后使用foreach循环我让它们掉下来。我只是想执行组合。这意味着每当我杀死一个僵尸时,combo_counter会增加。在杀死两个连续的僵尸时,combo_counter会转到2但是如果我点击屏幕上的任何其他位置,combo_counter应该转到0.
所以我的问题是如何检测我是否没有点击僵尸并点击屏幕上的任何其他位置。我附加了我的代码cctouchbegan方法
僵尸是一个CCArray所有僵尸精灵是存储
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
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...
}
}
这是一个迟到的回复 - 所以如果它已经解决了,那就告诉我闭嘴。
我根本无法理解你在代码中想要做什么 - 但是我会做什么:
在接触开始时,将触摸的位置存储在某个地方的某个属性中(可能在图层中?)并且在touchesbagen方法中没有更多的处理。
某处(可能在Layer-> update()方法中):
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):
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
伪代码
if(zombie1)
{
....
返回
}
if(zombie2)
{
....
返回
}
// OMG它不是僵尸:-O
tapped_some_else = true;
pseudo code
if( zombie1 )
{
....
return
}
if( zombie2 )
{
....
return
}
//OMG it isnt a zombie :-O
tapped_some_else = true;