# Proper way for Scene/Layer singletons?

Proper way for Scene/Layer singletons?
0.0 0

#1

So I’m still a little lost on how to properly make singletons out of my main Scene/Layer (named GameScene, it derives from CCLayerColor).

I have a static function which returns a CCScene* named GetInstance and a static CCScene* variable for the GameScene:

``````// In the .h
static CCScene* GetInstance();

// In the .cpp
static CCScene* m_pGameSceneSingleton = NULL;

CCScene* GameScene::GetInstance()
{
if(m_pGameSceneSingleton == NULL)
m_pGameSceneSingleton = GameScene::scene();

return m_pGameSceneSingleton;
}
``````

Now, in the scene() function, everything is the same, but my question is, should I also make a static GameScene which would be the child so I can actually get the Layer in question?

For instance, have this

``````// In the .h
static CCScene* GetSceneInstance();
static GameScene* GetLayerInstance();

// In the .cpp
static CCScene* m_pGameSceneSingleton = NULL;
static GameScene* m_pGameSceneLayerSingleton = NULL;

CCScene* GameScene::GetSceneInstance()
{
if(m_pGameSceneSingleton == NULL)
m_pGameSceneSingleton = GameScene::scene();

return m_pGameSceneSingleton;
}

GameScene* GameScene::GetLayerInstance()
{
return m_pGameSceneLayerSingleton;
}
``````

The only problem is I don’t know what to do about the scene() function considering in my AppDelegate.cpp I do:

``````pDirector->runWithScene(GameScene::GetSceneInstance());
``````

So then what should this do to involve the singletons?

``````CCScene* GameScene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();

// 'layer' is an autorelease object
GameScene *layer = GameScene::node();

// add layer as a child to scene

// return the scene
return scene;
}
``````

Any help would be greatly appreciated. Examples would be even better.

#2

how to make cocos2d::scene class as singleton?
thank you

#3

Why do you need to do this?

#4

to manage multiple layers

#5

There is no need for a singleton here. Show us more of what you want to do so we can help.