0

I am a Cocos2d beginner so although there is a bit much code here. Some with experience should be able to see what I am doing wrong here. It Crashes on the NSAssert in backgroundLayer when I try to get a instance of the GameBackground class that is added as a child to this layer in the +(id)scene class method. It crashes cause it is Nil ( the GameBackground class). But I thought i initialized it in the +(id)scene method when asking for it´s node? But please correct me, my thinking is fuzzy regarding this matter.

static GameLayer *sharedGameLayer = nil;
+(id) scene
{
    CCScene *scene = [CCScene node];

    //GameLayer* layer = [GameLayer node];
    //[scene addChild:layer];

    CCNode *backgroundLayer = [GameBackground node];
    [scene addChild:backgroundLayer z:1 tag:LayerTagBackgroundLayer];

    CCLayer *cocosObjectsLayer = [HelloWorldLayer node];
    [scene addChild:cocosObjectsLayer z:2 tag:LayerTagGameLayer];

    CCLayer *userTouchInterface = [GameUserTouchInteface node];
    [scene addChild:userTouchInterface z:3 tag:LayerTagUILayer];
    return scene;
}

// To access this scene: MultiLayerScene* sceneLayer = [MultiLayerScene sharedLayer];
+ (GameLayer *) sharedLayer
{
    NSAssert(sharedGameLayer = nil, @"sharedGameLayer not available.");
    //if (sharedGameLayer == nil) sharedGameLayer = [[GameLayer alloc] init];
    return sharedGameLayer;
}


-(id) init
{
    NSLog(@"init called");
    self = [super init];
    if (self)
    {
        NSAssert(sharedGameLayer == nil, @"another GameLayer is already in use!");
        sharedGameLayer = self;


        // uncomment if you want the update method to be executed every frame
        //[self scheduleUpdate];
    }
    return self;
}


// MultiLayerScene* sceneLayer = [MultiLayerScene sharedLayer];
// To access this scene: GameBackgroundLayer* backgroundLayer = [sceneLayer backgroundLayer];
-(GameBackground *) backgroundLayer
{
    CCNode *layer = [self getChildByTag:LayerTagBackgroundLayer];
    NSAssert([layer isKindOfClass:[GameBackground class]], @"%@: not a BackgroundLayer!", NSStringFromSelector(_cmd));
    return (GameBackground *)layer;
}

Then trying to get an instance of the background layer from another class:

    GameBackground *backgroundLayer = [GameLayer sharedLayer].backgroundLayer;
    [backgroundLayer runAction:sequence];
4

1 回答 1

1

I'm guessing the assertion that is failing is.-

NSAssert(sharedGameLayer = nil, @"sharedGameLayer not available.");

You're assigning nil to sharedGameLayer instead of comparing (==). Also, you've commented the line where you set sharedGameLayer. Just guessing here, but you may want to do something like this.-

+(id) scene
{
    CCScene *scene = [CCScene node];

    sharedGameLayer = [GameLayer node];
    [scene addChild:layer];

    return scene;
}

Leave your static constructor as clean as possible.

+ (GameLayer *) sharedLayer
{
    return sharedGameLayer;
}

-(id) init
{
    NSLog(@"init called");
    self = [super init];
    if (self)
    {
        CCNode *backgroundLayer = [GameBackground node];
        [self addChild:backgroundLayer z:1 tag:LayerTagBackgroundLayer];

        CCLayer *cocosObjectsLayer = [HelloWorldLayer node];
        [self addChild:cocosObjectsLayer z:2 tag:LayerTagGameLayer];

        CCLayer *userTouchInterface = [GameUserTouchInteface node];
        [self addChild:userTouchInterface z:3 tag:LayerTagUILayer];

        // uncomment if you want the update method to be executed every frame
        //[self scheduleUpdate];
    }
    return self;
}

Create your nodes tree in the init method, and add them as children of your root layer (sharedGameLayer) instead of scene object.

于 2013-04-15T21:41:01.950 回答