Accessing Layers in Init() and other functions

Hi All

Quick Question as has always plagued me

We add our layers (Or single layer in my case) in the create scene function. We use “auto”. Now we cannot access that layer in other functions

1

I tried adding “cocos2d::Layer* mainLayer;” to the header file and dropping “auto” but I get a error “a nonstatic member reference must be relative to a specific object”.
How can we add a variable layer in header file and then add that to the scene in the create scene fucntion

Additional related question to be clear

Scene* TestScene::createScene(){

	auto scene = Scene::create();
	auto layer = TestScene::create();
	scene->addChild(layer);
	return scene;

}

The code above seems to be a standard set up with a scene. i remember then always using the next piece for code throughout the init function
this->addchild()
What does “this” point to then? What are we adding this child to (Layer or scene). I understand this and that in Java as to what it means. I assume same concept.

So again to be clear, how can we make that layer accessible to all the class functions ? How do we add more layers. If we create another layer(Which i can do), how do we add it to the scene correctly(this->addchild(layer, 1)??)

Links to reading are fine. I did search and never found anything specific to this and i find it hard make sense of the class structure in relation to this

Thanks

At least two options:

  1. Set tag/name on the Layer. Then just access it through the scene instance pointer, or acquire that from the director’s current running scene.
  2. You’ll have to derive your own subclass from MyScene : Scene. Then you can create the layer(s) inside your derived scene’s init() method, or in another instance method.

p.s. The error stems from trying to access an instance member inside a static method createScene()

good and informative post

Thanks very much for answer. Ill put this to good use now.

One more question if you do not mind?

Look again at my picture !

If now in the init function I use “this”(this->addchild())… What does this refer to ? The layer of the scene ?

this is the instance of TestScene that was returned from TestScene::create().

Assuming TestScene is derived from Scene, you don’t actually need the anonymous parent scene. And actually while it probably compiles and runs without issue, there is supposed to really only be one Scene in any node graph/hierarchy.

// assuming 
class TestScene : public Scene {};
// simplify to return only one scene, and not nested scenes
Scene* TestScene::createScene() {
	auto scene = TestScene::create();
	return scene;
}
bool TestScene::init() {
  // `this` is an instance of TestScene (also instance of Scene)
  // could add Nodes here
}

The reason your code has the variable named ‘layer’ is that most cocos2d tutorials or templates show how to create a custom class that derives from Layer or Node that you can add into the scene that you pass to the director to start running.

// assuming 
class TestLayer : public Node {}; // no longer need to use the Layer class
// create anonymous scene for API and add this layer into it
Scene* TestLayer::createScene() {
	auto scene = Scene::create();
	// create `TestLayer` instance
	auto layer = TestLayer::create();
	layer->setTag(kTagTestLayerNode); // can set tag here
	scene->addChild(layer);
	return scene;
}
bool TestLayer::init() {
  // here `this` is the TestLayer instance (also Node instance)
  this->setTag(kTagTestLayerNode); // or set tag here
}
// in app delegate or other start up code
{
  // create (anonymous) scene that has TestLayer instance as child
  auto scene = TestLayer::scene();
  director->runWithScene(cocos2d::TransitionFade::create(0.5f, scene));
}

Not sure if that makes sense, but it’s the more technical/pedantic explanation.

The reason to not derive a class from Scene, and instead derive from Node/Layer, is that while it’s technically also derived Node, it’s philosophically just a wrapper and again philosophically there should only exist one Scene in the director’s current node hierarchy. Also, as you’re figuring out usually you want to create various Node-derived classes and access their instances with this instead of having to use getChildByTag or whatever means to access the individual nodes in the scene. In the end it’s really mostly just set up to help organize your code slightly, afaict.

Except one thing, Scene instances are used to encapsulate an entire game state such as “the main menu”, “the actual game running”, “the options screen”, “sometimes the pause/results/etc sub-screens”, etc. The Director requires a Scene (or derived) class instance to run as sort of the master root node.

Anyway, hopefully didn’t confuse you more.