rudyryk
rudyryk

Reputation: 3805

How to add global CCLayer which is not affected by scene transitions in Cocos2d?

Is it possible to add global layer in Cocos2d, which is not affected by scene transitions?

As I can see, it should be above all scenes hierarchy.

There's an old and short discussion on Cocos2d forum, but there's no answer: http://www.cocos2d-iphone.org/forum/topic/8071

UPD. 'by scene transitions' I mean 'by animated scene transitions'.

Upvotes: 1

Views: 1106

Answers (3)

Gerald
Gerald

Reputation: 71

You can use the notificationNode property of CCDirector to place a CCNode (ie.CCLayer, CCLabel, etc.) that will remain above scenes, even during transitions. Something like this:

CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Test" fontName:@"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)

It's meant for notification purposes (ads, etc.) so I wouldn't suggest trying to do anything too fancy from this node.

Upvotes: 3

CodeSmile
CodeSmile

Reputation: 64478

You can create a CCLayer subclass and turn it into a singleton. You add it to the scene like any other child node.

Whenever you transition from one scene to another, you can then remove the layer from the old scene and add it to the new scene as a child. This will only work if you don't use scene transition animations.

The alternative is to not use the CCDirector replaceScene method, and instead design your app to run as a single scene that never changes. To "fake" the changing of a scene you will use two layers, one global layer and another layer that contains your current scene nodes. If you want to transition you can animate the layer with CCActions to, for example, slide out of the screen while sliding in a new layer with a different node hierarchy. All you really lose is the convenience of the CCSceneTransition classes for animating scene changes.

Upvotes: 1

badgerr
badgerr

Reputation: 7982

My gut says no, my brain says maybe.

The documentation says "It is a good practice to use and CCScene as the parent of all your nodes."

I can't test this right now, but looking at the inheritence diagram of CCNode, it looks like the logic of CCNode and CCScene differs only by anchor point. So, you might be able to create a CCLayer to use as your root layer, and add two children to it - A root CCScene, and a CCLayer for your GUI (with a higher Z order).

However, scene transitions may still be tricky, as you generally call CCDirector replaceScene, which works on the root scene you give it. If you give it the CCScene child of your root CCLayer, it may not draw the CCLayer and its GUI child. If you give it the root CCLayer, you are in the same situation as before.

I would still give it a try.

Upvotes: 1

Related Questions