0

Hi fellow cocos2d game devs, i've a simple problem

I am implementing a CCScene that has two CCLayers on it, one is the game and the other is the HUD implemented as so:

+ (CCScene *)scene {
    CCScene *scene = [CCScene node];
    LevelLayer *layer = [LevelLayer node];
    LevelHUDLayer *hud = [LevelHUDLayer node];
    layer = [layer init];

    [layer setHUDLayer:hud];
    [hud setParentLevel:layer];

    [scene addChild:hud z:HUD_ZLevel];
    [scene addChild:layer];

    return scene;
}

And that works great. I also have a UIPinchGestureRecognizer that I implement as so:

- (void)onEnterTransitionDidFinish {
    pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(makePinch:)];
    [[CCDirector sharedDirector].view addGestureRecognizer:pinch];
}

And then I catch the gestures later on in code like so:

self.scale = pinch.scale;

This works flawlessly EXCEPT I want to ensure that only the game layer picks up the gestures and not the HUDLayer as well. Right now touching the HUDLayer's controls and trying to move the character around on the screen causes the level to scale, it's quite annoying.

So my question is how do I assign ONLY the LevelLayer to pick up the Gesture Recognizer? I think this would be easy to do if I could access the CCLayer's UIView but it seems as though I can only access CCDirector.sharedDirector.view

Thanks ahead of time!

Parad0x13
  • 2,007
  • 3
  • 23
  • 38

1 Answers1

0

You cannot get access to CCLayer's view for the one simple reason: it does not exist. All cocos2d logic is made inside single openGL view.

To restrict gesture recognizer's area you can implement delegate method gestureRecognizerShouldBegin:. There you can check positions of touches and return NO if they are over your HUD, not game layer.

Another way is to implement your own gesture recognizer using CCTouchDispatcher from cocos2d.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • Thanks for the input, I'm trying the gestureRecognizerShouldBegin way now but I'm wondering how to check if a childlayer has received input. This is important since the HUD controls have transparent parts that allow touches to propagate through – Parad0x13 Nov 17 '12 at 02:38