iphoneioscocos2d-iphonesimpleaudioengine

How to prevent sprites from being detected within a particular area (which is also a sprite) in Cocos2D?


I want to prevent sprites from being detected within a particular area in Cocos2D. In my game, sprites are spawning from the bottom which gets detected on touch. I have added a newArea (an area) where sprites spawn from beneath the newArea (as z orientation is 1 for newArea). Now in this particular area I don't want touch to be detected (as I have to add additional features here). How can I do so? Following is the code i am using.

    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];

//For touch detection

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

Solution

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

    I think you can carry on from here on your own. ;)