Search code examples
c++svgcocos2d-x

override function in cocos2dx


I am creating level based game. There is difficult to override SVG file in different level.

Here is the coding,

HelloWorld.cpp

 const char* HelloWorld::SVGFileName()     //virtual function
 {
   return NULL;
 }

CCScene* HelloWorld::scene()
{
   CCScene *scene =  CCScene::create();
   HelloWorld *game = HelloWorld::create();
   GameHUD *hud = GameHUD::HUDWithGameNode(game);
   game->hud_ = hud;
   scene->addChild(game);
   scene->addChild(hud, 10);
   return scene;
 }

level1.h

 class CC_DLL level1: public HelloWorld
 {
 public:
   level1();
   ~level1();
    const char* SVGFileName();
    void addBodyNode(BodyNode* node,int zOrder);
    void initGraphics();
 };

level1.cpp

 const char* level1::SVGFileName()
 {
    CCLog("LevelSVG: level: override me");
    return ("test3.svg");
 }

SelectLevelScene.cpp

void SelectLevelScene::level0()
{
   CCDirector::sharedDirector()->replaceScene(level1::scene());
}

My problem is,

I am not able to override the function SVGFileName() in level1.cpp. Is there any problem in my code?

Any idea to fix it?


Solution

  • You return a const char* created in the function's local scope. This means as soon as the function returns the returned pointer is garbage.

    You should return std::string instead.