这是我的代码,用于检测是否触摸特定的精灵
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for(CCSprite *sprite in shapeArray)
{
if(CGRectContainsPoint(sprite.boundingBox, location))
{
//There is a sprite that is touched
mSpriteOnHand = sprite;
currentPoint = mSpriteOnHand.position;
break;
}
//This part didn't work
else
{
NSLog(@"Touch outside);
}
}
}
现在我想检测触摸是否在外面(不在任何精灵或空白区域),但我不知道该怎么做。
答案 0 :(得分:0)
如果您的触摸工作正常,我认为在for循环之外添加BOOL然后添加if语句,就像这样可行。
BOOL itemFound = NO;
for(CCSprite *sprite in shapeArray)
{
if(CGRectContainsPoint(sprite.boundingBox, location))
{
//There is a sprite that is touched
mSpriteOnHand = sprite;
currentPoint = mSpriteOnHand.position;
NSLog(@"item TOUCHED");
itemFound = YES;
break;
}
}
if (itemFound == NO)
{
NSLog(@"Touch outside");
}