Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:33:44+00:00 2026-05-29T12:33:44+00:00

How to do you make Gesture Recognizers only accessible from one scene? I have

  • 0

How to do you make Gesture Recognizers only accessible from one scene?

I have a simple cocos2d app with two scenes. I create a UITapGestureRecognizer like so in AppDelegate:

CCScene *gameScene = [GameLayer scene];
GameLayer *gameLayer = (GameLayer *) [gameScene.children objectAtIndex:0];

// Capture a tap
UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:gameLayer action:@selector(handleTapFrom:)] autorelease];
[tapRecognizer setDelegate:viewController];
[viewController.view addGestureRecognizer:tapRecognizer];

[[CCDirector sharedDirector] runWithScene:gameScene];

That works great, I get the touch event, and everything is dandy. However, I want to switch to another scene for my pause screen, which I do like so:

    PauseMenuScene *pauseMenu = [PauseMenuScene node];
    [[CCDirector sharedDirector] replaceScene:pauseMenu];

When that scene launches however, if I touch anywhere on it it crashes. This seems to be because the touches are going to gameScene, which is in stasis.

How do I prevent this?


After andrewx confirmed my suspicion, I was able to solve this. I still couldn’t figure out how to add UIGestureRecognizers to individual scenes, but what I did instead was send the gestures to my delegate directly, and then have it parsel them out to my scenes. Here’s a snippet:

Create the gesture recognizer in AppDelegate (changed to direct to self):

UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)] autorelease];
[tapRecognizer setDelegate:viewController];
[viewController.view addGestureRecognizer:tapRecognizer];

Next, I made sure that I actually had a CCScene class for my main class (I was just going off the base HelloWord class so it only had a CCLayer class). That class looks like:

@implementation GameScene
@synthesize layer = _layer;

-(id) init
{
    if (self = [super init])
    {
        self.layer = [GameLayer node];
        [self addChild:_layer];
    }

    return self;
}

-(void) dealloc
{
    [_layer release];
    _layer = nil;
    [super dealloc];
}

@end

And finally, I delegated the taps:

-(void) handleTapFrom: (UITapGestureRecognizer *) recognizer
{
    CCScene *currentScene = [[CCDirector sharedDirector] runningScene];

    if ([currentScene isKindOfClass:[GameScene class]])
    {
        GameLayer *gLayer = ((GameScene *) currentScene).layer;
        [gLayer handleTapFrom:recognizer];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:33:46+00:00Added an answer on May 29, 2026 at 12:33 pm

    Your gesture recognizer is initialized with a target of one scene, but when you replaceScene, that target no longer exists, right? So your gesture recognizer is trying to send touches to a layer that is not there. I suspect this is the cause of your crash.

    I recommend adding your touch controls to the individual cocos classes so each scene can handle touches on its own. When that scene is released and replaced by another one, it has its own touch recognizers also. This is much safer than adding touch controls to the App Delegate, which really isn’t the place (usually) for this type of scene-specific logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app I have a listview with news items showing only a summary,
As MPMoviePlayerViewController supports pinch gesture ( two fingers move apart ) to make the
Hi I want to create aplication which loads large image, and simple gesture I
I am displaying two images in my iPhone app, one on top, one on
I have just discovered the Gesture Recognizers in IB and I would like to
I'm working on a simple note-taking/painting app. I have a view controller that works
make is not only useful for building your programming project, but it seems to
I make a lot of web applications and from time to time I need
CODE I have some code that adds a UILongPressGestureRecognizer gesture recognizer called _recognizer to
I have a listbox which has all the names for a list of Gesture

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.