i’ve got a “Scene” class that adds several layers. I would like to add a last layer at the end of the game (to show the different scores) from one of the layer (so this layer would call the Scene class with a delegate, and the Scene class should add this new layer: “LevelCompleteLayer”).
But the method in the Scene class does not receive the call. Would you know why? The other delegates (between the layers) work fine, but this one (from the layer to the Scene) does not.
Here’s the code :
//in Level1Scene.h :
@interface Level1Scene : CCScene <CompleteLayerDelegate>{
//in Level1Scene.mh :
@implementation Level1Scene
@synthesize levelComplete;
-(void)showLevelCompleteLayer {
CCLOG(@"delegateCompleteLayer showLevelCompleteLayer!!!");//does not show up
[self addChild:levelComplete z:5000];//is not added
}
-(id)init {
if ((self = [super init])) {
ScoreLayer *scoreLayer = [ScoreLayer node];
layer = [[Level1Layer alloc] initWithBackgroundImage:background.backgroundImage];
levelComplete = [[LevelComplete alloc] init];
layer.delegate = scoreLayer;//works fine
layer.delegateCompleteLayer = self; //does not respond
scoreLayer.delegate = layer;//works fine
//...
}
return self;
}
//in Level1Layer.m :
[delegateCompleteLayer showLevelCompleteLayer];
//in GameProtocols.h :
@protocol CompleteLayerDelegate
-(void)showLevelCompleteLayer;
@end
Thanks for your help
As a way to discover why it is not working, I would suggest:
add a breakpoint (or a trace) in the method (let’s call it method A) where you execute
[delegateCompleteLayer showLevelCompleteLayer];?if it is called, inspect
delegateCompleteLayervalue;if it is not, set a breakpoint in the method that should call method A;
(repeat, similarly to 2/3).
EDIT:
If I understand correctly, when you execute
[delegateCompleteLayer showLevelCompleteLayer];, instead of jumping into that method, you jump to a random (more or less) memory address.It seems to me that this is a hint to a memory management issue. It seems like your
delegateCompleteLayerhas become garbage at the moment when you callshowLevelCompleteLayerfromLevel1Layer.You could inspect your code and make sure that the delegateCompleteLayer is properly retained and that its dealloc method is not called early (put a breakpoint or a trace in there).
Another thing you could try is enabling zombies detection. Please, read this to know how to do it.
EDIT 2:
About your second attempt with the delegate, I think that when you do:
your
delegatemight have been not set yet (in theinitmethod), so that explain why the call is void.More generally, I suggest that you put NSLog traces in all of your
deallocmethods:so that you can detect any unforeseen deallocation; and also before each call to a delegate that does not work:
to check that your are calling the right object.
Hope it helps.