1

我想防止在 Cocos2D 的特定区域内检测到精灵。在我的游戏中,精灵从底部生成,触摸时会被检测到。我添加了一个 newArea(一个区域),精灵从 newArea 下方生成(因为 newArea 的 z 方向为 1)。现在在这个特定区域,我不希望检测到触摸(因为我必须在此处添加其他功能)。我该怎么做?以下是我正在使用的代码。

    CCSprite *background = [CCSprite spriteWithFile:@"bgmain.png"];
           background.position = ccp(winSize.width/2, winSize.height/1.7);
           [self addChild:background];

    CCSprite *newArea = [CCSprite spriteWithFile:@"newImage.png"];
          newArea.position = ccp(winSize.width/2, winSize.height/17);
          [self addChild:newArea z:1];

//用于触摸检测

- (void)selectSpriteForTouch:(CGPoint)touchLocation 
{

    for (CCSprite *sprite in targets)
    {

        if (CGRectContainsPoint([sprite boundingBox], touchLocation))

        {
            switch (sprite.tag) {
                case 1:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button1.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                  case 2:  
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button2.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 3:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button3.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 4:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button4.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 5:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button5.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 6:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button6.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 7:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button7.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 8:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button8.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 9:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button9.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                case 10:
                    [[SimpleAudioEngine sharedEngine] playEffect:@"button10.wav"];
                    [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
                    break;
                default:
                    break;
            }

            NSLog(@"target.tag %d",sprite.tag);

            [targets removeObject:sprite];

            [self balloonBlastAnimation:sprite];

            [sprite.parent removeChild:sprite cleanup:YES];

            break;

        }
    }
}

-(void) removeSprite:(CCSprite*) s
{
    s.visible = FALSE;
    [self removeChild:s cleanup:YES];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    for( UITouch *touch in touches ) 
    {
        CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
        [self selectSpriteForTouch:location];   
        NSLog(@"touch was detected");
    }   
}
4

2 回答 2

1

在 touchesBegan 上,检查触摸是否在要忽略的矩形中,如果是,则返回。

于 2011-10-31T08:12:18.560 回答
1
CGRect bbox = CGRectMake(0, 0, 
                        newArea.contentSize_.width, newArea.contentSize_.height);
CGPoint locationInNodeSpace = [self convertToNodeSpace:touchLocation];
BOOL touchOnNewArea = CGRectContainsPoint(bbox, locationInNodeSpace);

我想你可以自己从这里继续。;)

于 2011-10-31T10:15:58.543 回答