0

这是我检测是否触摸特定精灵的代码

- (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);
    }
}

}

现在我想检测触摸是否在外面(不在任何精灵或空白空间上),但我不知道该怎么做。

4

1 回答 1

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");
}
于 2013-06-15T12:58:32.523 回答