Layers and Scenes logic

Hello!!

I have some questions about scenes and layers. In every Cocos2D-X example I see how a class extending from layer creates an scene and then it attaches itself to that scene. My question is, is not better to make a class extending from Scene and then another class extending from Layer and attach this one to the first class extended from Scene? In that way, then, I could create some other layers to have in the same Scene.

For example, GameScene that implements from cocos2d::Scene. And, inside this class I could instantiate and add all the layers I want (PauseLayer for the game, HUD layer, GameLayer, …).

Now another question: It is posible to FadeIn and FadeOut a Layer to set it to visible or invisible?

Thank you very much!!

Bye!!

There are a lot of approaches. I don’t follow this approach either. I tend to not subclass a lot either. Instead I use member variables.

Instead of:

class HelloWorld : public cocos2d::Layer
{
// stuff
}

I do:

class HelloWorld
{
public:
    inline cocos2d::Layer* getLayer() { return myLayer; };

private:
    cocos2d::Layer* myLayer;
}
2 Likes

Thanks for the reply!!

What you do is a good way too and logical!! I like it!! But, why do you do that? Is it your personal choice or is because is better for the program logic?

Thanks!! Bye :smiley:

I guess it is just what I prefer.

Hi @slackmoehrle, quick question. In a similar post here:

you say exactly the opposite. Is this because in that other post you are talking about Sprites?
So for Sprites you use inheritance and for Layers you use composition?
I’m just trying to figure out which way to do it, that’s all.
Last thing, in this example with the Layer, would you mind sharing the constructor and destructor methods? because I understand the autorelease() in Cocos Nodes handle some memory management for us, but in this example you HelloWorld class is a plain class, that is why I am interested in the constructor and destructor methods.

Thanks very much in advance.

Some of my posts are very old.

I used to use inheritance. I have switched the way I work to composition.

I still have code that uses inheritance and I don’t bother to change unless I rewrite something. Even then if my change is simple I don’t re-write.

I just make a normal cocos2d::Layer(), IIRC.

If I can help more, just ask.

Hi @slackmoehrle, thanks for your quick reply. The inheritance comment is indeed older than the composition one, so that makes sense. So back to the composition example:

  • is it possible that you update the example with the constructor and destructor for the HelloWorld class? how do you free the memory?

delete myLayer;

???

Thanks again for all the help.

Sorry, just to clarify, what type of example do you need?

Constructor and destructor for HelloWorld class in your example, focusing on memory management, and what happens with the Layer object you have in there. Is that ok?

Sorry, sub-classing or composition? I would do these entirely different ways.

I am talking about composition, since the inheritance is easier because the framework takes care of the memory allocation with create() functions, I think… is that correct? Thanks.