Question

What is the role of the scene in Cocos2d?

The scene instantiates layers, like for instance a GameEngineLayer, a HUDLayer, etc.

I guess the GameEngineLayer class can be identical between scenes, but there will of cause be different objects in different scenes.

In scene1 there may be one Santa, and one Rudolf, whereas in scene2 there are just two Santa. Shall scene1 instantiate a santa, and one rudolf and pass them in a list of game objects to its instance of the gameEngine? And scene two instantiate two santa following the same pattern?

Shall the scenes also pass a list of events down to their respective gameEngine instance with time stamped events? For instance that santa shall feed Rudolf after a one minute?

Is the responsibility of the scene to do these kind of things?

I've started with a cocos2d/box2d game one week ago, so I'm a beginner. I've read lots of examples, but they usually code everything in a HelloWorldLayer class. :)

Added example: http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d The scene is defined inside ActionLayer.mm. Why?? Why not have Scene1.m that instantiates the ActionLayer?

Was it helpful?

Solution

The only reason why there's a CCScene in cocos2d is because CCDirector requires it as the base class for methods like runWithScene and replaceScene. Other than that, CCScene, CCLayer and CCNode are virtually identical to each other.

You can give scenes, layers, nodes, sprites, etc. any role you want.

But typically the Scene assumes the role of the state manager of the currently active game objects (nodes). A common use case is to declare the scene a singleton so that any child node can access the scene's base methods, for example to send other unrelated nodes messages, or to check if the game is over, and so on.

For propagating events I'm in favor of passing events down, since there's no easy way to pass events up as there is in UIKit. However it depends on what kind of events and what type of design you prefer.

Whether Santa sends a message to Rudolf in order to feed it, or Rudolf frequently asking Santa whether it is has something to eat, is also up to you. There are pros and cons to both approaches.

The reason why many examples instantiate the CCScene object inside the CCLayer class is simply because of that requirement of Cocos2D having to have a scene. Personally I think it's a bad case of a bad example that has become a de facto standard over the years. It would have been better to always create a CCScene class, and inside that create all the layers and other nodes that you want. It would have made the relation between scenes and layers and other nodes more obvious. As it is, there's many many projects out there whose scene class simply does nothing but contain a single layer that runs all the code.

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