Re-enabling touch detection on Layers in cocos2d

168 views Asked by At

I am using cocos2d 2.1 version to code up a simple board game on iOS. The logic requires disabling touch detection on individual layers for a predefined period of time. While disabling touch detection on individual layers works just fine I am having a lot problems re-enabling the touch detection.

I disable the touch detection using touchEnabled property

layer.touchEnabled = NO;

I try later to reverse this using the same property

layer.touchEnabled = YES;

But this does not seem to work at all and none of the tap events are any longer propagated to the layer..

What am I doing wrong here? Is there something I am missing??

2

There are 2 answers

0
mohitdream4u On

you have to give dispatcher delegate again, this may help you

-(void)touchActive
{
    if (self.touchEnabled)
    {
        return;
    }

    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self
                                                          priority:0
                                                   swallowsTouches:YES];
    self.touchEnabled = YES;

}
1
Franek Kuciapa On

Resolved.

It turns out I needed to set the touchMode to kCCTouchesOneByOne in the initializer of my layer before setting initial touchEnabled to YES. Otherwise, wrong dispatcher was getting installed inside CCLayer and was messing things up. I needed the one with swallowing touches set to YES.

layer.touchMode = kCCTouchesOneByOne;
layer.touchEnabled = YES;