Search code examples
c++cocos2d-x

Error 'CCScene' does not name a type in Cocos2d-x


I got this error 'CCScene' does not name a type and 'class SceneManager' has no member named 'createScene'

My code is below. Can you tell me where I'm wrong? Thanks

SceneManager.h

class SceneManager : public cocos2d::CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
    CCScene* createScene(int sceneID);
};

SceneManager.cpp

CCScene* SceneManager::createScene(int sceneID){
    CCScene* pScene = NULL;

    switch (sceneID){
        case LOGO_SCENE:
        {
            pScene = CCScene::create();
            // LogoScene pScene = new LogoScene();
            break;
        }   
        default: break;
    }

    return pScene;
}

CCScene* SceneManager::scene(){
    CCScene *scene = CCScene::create();
    return scene;
}

bool SceneManager::init(){
    if (!CCLayer::init())
    {
        return false;
    }
    return true;
}

And then, I call to create scene in the AppDelegate.cpp

...
// create a scene. it's an autorelease object
    SceneManager *pSceneManager = new SceneManager();
    CCScene *pScene = pSceneManager->createScene(LOGO_SCENE);

    // run
    pDirector->runWithScene(pScene);

Solution

  • I think that you forgot using namespace cocos2d.

    SceneManager.h

    class SceneManager : public cocos2d::CCLayer
    {
    public:
      virtual bool init();  
      static cocos2d::CCScene* scene();
      cocos2d::CCScene* createScene(int sceneID);
    };
    

    SceneManager.cpp, AppDelegate.cpp

    Make sure using namespace cocos2d; or USING_NS_CC; or prepend cocos2d:: for cocos2d-x classes.