Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d GestureRecognizer not working after finishing a Level and restarting it

I'm currently building a game with cocos2d and I have the following Problem: I have a MenuScene, where the user can start the game. When he does so, the gestureRecognizer gets initialized with the level in the following way:

CCScene *scene = [LevelScene scene];
LevelScene *layer = (LevelScene *) [scene.children objectAtIndex:0];
UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];

Everything works fine, and after the level is finished, the user sees a gameOverScene and is sent back to the MenuScene. When I start the game again, everything works (ingame animations, level is displayed, etc.) besides the fact, that the gestureRecognizer isn't recognizing any touches anymore. Do you have any idea why that might be or how to debug this?

Thanks.

like image 491
dschihejns Avatar asked Nov 29 '25 22:11

dschihejns


1 Answers

In all likelihood the gestureRecognizer retains the target. I've ran into a similar problem which caused the "target" scene that was a delegate of a UIKit class not to be deallocated. Ie the entire scene was leaked.

Due to some unfortunate circumstances the original scene's selector still got called but wasn't processed because the scene was forever locked in a "game over" state. My guess is that something like that is happening to you.

Question: do you relese the gestureRecognizer before changing from the LevelScene to another scene? If not, you absolutely should fix that!

like image 116
LearnCocos2D Avatar answered Dec 01 '25 12:12

LearnCocos2D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!