2011-10-31 43 views
1

我想防止在Cocos2D的特定區域內檢測到精靈。在我的遊戲中,精靈從底部產生,並在觸摸時發現。我已經添加了一個newArea(一個區域),其中sprite從newArea下方產生(因爲z方向對於newArea是1)。現在,在這個特定的區域,我不希望觸摸被檢測到(因爲我必須在此添加其他功能)。我該怎麼做?以下是我正在使用的代碼。如何防止在Cocos2D的特定區域(也是精靈)內檢測到精靈?

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

回答

1
CGRect bbox = CGRectMake(0, 0, 
         newArea.contentSize_.width, newArea.contentSize_.height); 
CGPoint locationInNodeSpace = [self convertToNodeSpace:touchLocation]; 
BOOL touchOnNewArea = CGRectContainsPoint(bbox, locationInNodeSpace); 

我想你可以從這裏繼續自己。 ;)

+0

非常感謝。它幫助:) –

1

上的touchesBegan,檢查觸摸是要忽略的矩形,如果是,就返回。

+0

我想在這裏我要使用CCTouchDispatcher功能,但不知道在哪裏實施。 –

+0

CCSprites本質上是矩形,它們表示的圖像是否是矩形。 –