Pablo
Pablo

Reputation: 29519

Modal dialog with buttons failing

I have layer, on which I placed sprite(yellow) and child menu("Main Menu: button), to look like modal dialog. There are also number of buttons, menus below that layer. What I want to achieve is whenever this layer is presented, do not propagate touch event to other items. This is what I've done in layer class:

- (id)init {
...
    // I was hoping that this will take touch and eat, so if I tap on button it will not be propagated further, because it has lowest priority
    [_menu setHandlerPriority:INT_MIN+1];
...
}    
-(void)registerWithTouchDispatcher {
    // With this I was hoping that if I tap anywhere else my layer class will capture it first
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:INT_MIN+2 swallowsTouches:YES];
}    
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // so I simply drop any tap on my layer
    return NO;
}

Probably I'm missing something, but with this buttons below my layer are still getting the touch. So the modal concept doesn't work.

How can I make all child items of my yellow sprite touchable and the rest not touchable.

enter image description here

Upvotes: 0

Views: 85

Answers (1)

Lorenzo Linarducci
Lorenzo Linarducci

Reputation: 541

Your return in ccTouchBegan is the issue.

Your layer will only swallow touches when ccTouchBegan tells it to (returns YES).

Normally you would check if the touch is within the bounds of the layer and then return YES, but in this case, returning YES always will swallow all touches (unless there is another layer of priority INT_MIN, or INT_MIN+1).

Edit: And point 2 - make sure you're enabling touches to begin with:

[self setTouchEnabled:YES];

Upvotes: 1

Related Questions