Search code examples
cocos2d-iphone

Temporarily disable touch with CCLayerPanZoom


I've recently added the CCLayerPanZoom cocos2d extension to my project and got my game scene zooming and scrolling just like I want. Now when the player takes certain actions, I want to be able to disable the pan/zoom temporarily while they perform an action but can't figure out how to do it. I searched around and found the following code in a forum but it doesn't work or I don't know how to use it.

Does anyone know how to do this properly either with different code or the code below?

-(void)enableTouches:(BOOL)enable {

    if(enable) {
        [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0];
        _panZoomLayer.isTouchEnabled = YES;
        CCLOG(@"LayerPanZoom enabled.");
    } else {
        [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
        _panZoomLayer.isTouchEnabled = NO;
        CCLOG(@"LayerPanZoom disabled.");
    }
}

Solution

  • I finally figured it out and figured I would post the answer back up here to share. The code I posted wasn't working because I was sending back self instead of the _panZoomLayer. So here are the steps to get this working yourself.

    1. Implement the CCLayerPanZoom into your project as described by the documentation.
    2. Add the following code as a method to call on your new CCLayerPanZoom class.

      -(void)enableTouches:(BOOL)enable {
         if(enable) {
            [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:_panZoomLayer priority:0];
            CCLOG(@"LayerPanZoom enabled.");
         } else {
            [[CCTouchDispatcher sharedDispatcher] removeDelegate:_panZoomLayer];
            CCLOG(@"LayerPanZoom disabled.");
         }}
      

    NOTE: Make sure to put the instance of the parent class as the delegate to remove.

    1. In order to re-enable and have it function properly, you have to remove all the entries from the array in the CCLayerPanZoom class before calling to re-register the delegate. I created a new method in the CCLayerPanZoom class as follows and just call it right before the addStandardDelegate method above.

      -(void)removeTouchesFromArray { [self.touches removeAllObjects]; }

    Then it all works great! Took me a while to learn how to use this extension but it works perfect once you figure it all out. I can single finger pan, double finger zoom/pan, set center location for entire scene, limit panning past edges, and set min/max scales. I know people have had a lot of issues with this but it is a great extension, just takes some messing around with to understand it. Let me know if you have any questions. Hope this helps someone else.