0

I am working with SpriteBuilder and Cocos2d to build a simple game, and I want to display an error message inside an if statement.

My problem is trying to initialize the CCNode I created in SpriteBuilder to show up on-screen.

I tried creating a CCNode layer and just creating all the objects via SpriteBuilder, but wasn't exactly sure how I was supposed to get that to show up on-screen as what I tried did not work correctly. I tried just using [self addChild:errorLayer] in the if statement and it crashed my app with the error message Argument must be non-nil, so I set up a breakpoint and errorLayer is nil, but I'm not sure how to make it non-nil.

I also tried creating a CCNode programmatically, but when the if-statement was run it didn't display anything on-screen. Here is the code I tried:

    CCNode *errorLayer = [[CCNode alloc] init];
    [errorLayer setContentSize:CGSizeMake(50, 100)];
    [errorLayer setColor:[CCColor redColor]];
    [self addChild:errorLayer];

Could anyone give me some tips on getting this to work? Thanks.

MainScene, which is the scene that the above code is called in, is initialized in AppController like this

- (CCScene*) startScene
{
    return [CCBReader loadAsScene:@"MainScene"];
}
Albert K
  • 25
  • 7
  • Post the code how you load the ccb. The above code is correct but it's just a CCNode that doesn't draw anything (color is ignored) as opposed to say a sprite or label node. When you do ccbreader load:@"folder/file" be sure to a) include folders in the string and b) omit the extension (or use .ccbi not .ccb) – CodeSmile Jun 21 '14 at 21:06
  • @LearnCocos2D I edited the OP to include how I load the scene that contains the above code. Do I have to use CCBReader to load the ErrorLayer before I add it as a child? – Albert K Jun 21 '14 at 21:36
  • Yes, if it's not already in the MainScene.ccb you have to load: and addChild: the errorlayer.ccb. That's the load code I was asking for but if you haven't been loading the ccb before that explains the issue. – CodeSmile Jun 21 '14 at 22:08

0 Answers0