0

This problem has been frustrating me for quite some hours. I basically have a game where I need to use UITapGestureRecognizer, but if there is a CCButton on screen, this tap gesture overrides a tap on the button. In order to get past this problem, I implement the method listed below in my code -(BOOL)gestureRecognizer. In order for this to be recognized, I have to set the UITapGestureRecognizer's delegate to self (at least as far as I know).

My button works fine on its own, no bugs. Without declaring the delegate of the UITapGestureRecognizer, the taps are recorded perfectly and everything works, no bugs (I just can't click on the CCButton then). When I implement this all together and set the delegate and do the following, I get a bug. I enter my LevelScene, tap my mainMenu button which brings me back to my main menu, then click my play button again, and then my app crashes with Thread 1: EXC_BAD_ACCESS (code = 1, address = 0xe000000c) in my main.m file.

I'm not sure why this is happening, and additionally I am using Spritebuilder, but I'm not sure if that would affect anything. Below is the relevant code (my Grid is added to my LevelScene through Spritebuilder). Sorry the code is long but I think it's all necessary.

main.m

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

    @autoreleasepool {
        //error occurs right below this line
        int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
        return retVal;
    }
}

LevelScene.m

@implementation LevelScene {
    Grid *_grid;
}

- (void)mainMenuFromLevelScene {
    [[LevelManager sharedInstance] reset];
    CCScene *levelScene = [CCBReader loadAsScene:@"MainScene"];
    [[CCDirector sharedDirector] replaceScene:levelScene];
}


@end

Grid.m

@implementation Grid

- (void)didLoadFromCCB {
    UITapGestureRecognizer * gridTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gridTapped)];
    [[[CCDirector sharedDirector] view] addGestureRecognizer:gridTapped];
    gridTapped.delegate = self;
}

- (void)gridTapped {
    //correctly logs Grid tapped when screen is tapped
    CCLOG(@"Grid tapped");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //everything works correctly here too
    CGPoint touchLocation = [touch locationInNode:self];    
    if ((touchLocation.y < 0 || levelWonBool)) {
        CCLOG(@"returning no");
        return NO;
    }
    return YES;
}

Grid.h

#import "CCNodeColor.h"

@interface Grid : CCNodeColor <UIGestureRecognizerDelegate>

@end
spaderdabomb
  • 942
  • 12
  • 28

0 Answers0