0

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

Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

2

As a way to discover why it is not working, I would suggest:

  1. add a breakpoint (or a trace) in the method (let's call it method A) where you execute [delegateCompleteLayer showLevelCompleteLayer];?

  2. if it is called, inspect delegateCompleteLayer value;

  3. if it is not, set a breakpoint in the method that should call method A;

  4. (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 delegateCompleteLayer has become garbage at the moment when you call showLevelCompleteLayer from Level1Layer.

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:

     //in the init :
     [delegate startGame];

your delegate might have been not set yet (in the init method), so that explain why the call is void.

More generally, I suggest that you put NSLog traces in all of your dealloc methods:

NSLog(@"DEALLOCATING OBJECT %@", [self description]);

so that you can detect any unforeseen deallocation; and also before each call to a delegate that does not work:

NSLog(@"CALLING INTO DELEGATE %@", [YOUR_DELEGATE_HERE description]);

to check that your are calling the right object.

Hope it helps.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • thanks sergio, i edited my post with some images, could you have a look? Thanks for your help – Paul Jun 29 '12 at 08:18
  • thanks sergio, it was exactly what you said, i called the delegate when it was "nil", now it works. thanks – Paul Jul 01 '12 at 05:41