Question

Using cocos2d for iPhone game development, I am confused between Layer and Scene. For example, My simple game have several "UI Pages" like main menu, highscores, game board etc.

So should I use Layer or Scene for every "UI page", and why?

Was it helpful?

Solution

Reviewing SpritesDemo.m/.h, it would appear that they are using Layer, and then creating new scenes, attaching the layer and then replacing the scene on the director

@interface SpriteDemo : Layer
@interface SpriteManual : SpriteDemo

The code then does the following:

-(void)nextCallback:(id)sender {
  Scene *s = [Scene node];
  [s add: [nextAction() node]];
  [[Director sharedDirector] replaceScene s];
}

So, in short, the answer to your question would be "both", you use Layer to represent your actual "UI Page", but you attach the Layer to a new scene and replace the current scene in the director.

OTHER TIPS

It's easy:

They have a CCLayer in those examples and a +scene method that makes a new scene and attaches the layer on it. Because a layer is not a "UI Page" as you named it. A scene is a "UI Page".

On both (scene and layer) you can add more layers as children. So you can stack up as many layers as you want. This is actually a good way to enabled multitouch - by creating a layer for each of your objects and enabling touches on those layers. Bottom line, the way they did it is right, so you should too work with layers and use that +scene method to attach the main layer to the scene.

Hope this helps. Sorry for my English. Not native.

You really don't need to use a Layer unless you're getting some kind of touch input(because it's the only class in Cocos2D that implements TouchEventsDelegate). The demos work fine if you put their code directly on the scene without the Layer, so as far as I can tell they only did that to demonstrate other Cocos2D functionality(adding children, setting positions, etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top