Cocos2d: gesture recognizers and CCMenu

365 Views Asked by At

I have been following this tutorial on integrating UIKit with a CCLayer. Basically all I want to do is to add gesture recognizers handlers to my layer and trigger my game actions according to those.

However I do have a problem (which doesn't seem new) as the CCMenu items I added to the layer are not absorbing the clicks/taps.

In brief: I do have a layer where I integrated all the code suggested by Ray and it works perfectly except that the CCMenu doesn't absorbe clicks.

I read the post but I am not quiet comfortable with the idea of modifying CCNode and adding the method to verify if the touch is in space etc..

I thought that the easier way (for me) would be to just pass the touch on the CCMenu if the touch is over the CCMenu area.

Here a code snippet:

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer{
    UIView * view = [[CCDirector sharedDirector] view];
    CGPoint point = [self convertYTouch:[tapRecognizer locationInView:view] ];

if (CGRectContainsPoint([pauseMenu boundingBox], point)) {
    [myMenu HowDoIPassTheTouchToTheMenu]        
}
else{
   //Handle single tap
}
}

Any idea on how I can pass the touch to the menu?

I tried to play around with TouchDispatcher and priority but can't get it to work.

EDIT: I wrote this method but doesn't seem to help much

-(void) activateItemForTouch:(CGPoint)point
{
    if( state_ != kCCMenuStateWaiting || !visible_ || ! enabled_)
        return;

    for( CCNode *c = self.parent; c != nil; c = c.parent )
        if( c.visible == NO )
            return;

    CCLOG(@"in activate item for touch");
    selectedItem_ = [self itemForTouchLocation:point];
    [selectedItem_ selected];

    [selectedItem_ unselected];
    [selectedItem_ activate];

    state_ = kCCMenuStateWaiting;
}

-(CCMenuItem *) itemForTouchLocation: (CGPoint) touchLocation
{
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];

    CCMenuItem* item;
    CCARRAY_FOREACH(children_, item){
        // ignore invisible and disabled items: issue #779, #866
        if ( [item visible] && [item isEnabled] ) {

            CGPoint local = [item convertToNodeSpace:touchLocation];
            CGRect r = [item rect];
            r.origin = CGPointZero;

            if( CGRectContainsPoint( r, local ) )
                return item;
        }
    }
    return nil;
}

EDIT BIS:

I also tried to implement the UIGestureRecognizerDelegate protocol but even if I set gestureRecognizer to FALSE it never passes the gesture/touch to the menu.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    CCLOG(@"should receive");
    return FALSE;
}
0

There are 0 best solutions below