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];