首页 > 解决方案 > 尝试使用 Cocos2D-x C++ 调用初始化函数

问题描述

我正在尝试重现此教程https://www.youtube.com/watch?v=-vb6QfatNQI&list=PLRtjMdoYXLf5H1O_AOZtc415UmSQCVs93&index=3&ab_channel=SonarSystems

但是我的 init 函数从未被调用,我检查了断点。

这是我在 SplashScene.cpp 上的代码的一部分:

Scene* SplashScene::createScene()
{
    auto scene = Scene::create();
    auto layer = Scene::create();
    scene->addChild(layer);

    return scene;
}


// on "init" you need to initialize your instance
bool SplashScene::init()
{
    //////////////////////////////
    // 1. super init first
    if (!Layer::init())
    {
        return false;
    }

    SonarCocosHelper::UI::AddCentredBackground(MAIN_MENU_BACKGROUND_FILEPATH, this);
    scheduleOnce(schedule_selector(SplashScene::SwitchToMainMenu), 5);

    return true;
}

这是我在 AppDelegate.cpp 上的代码的一部分(就像在教程中一样):

// create a scene. it's an autorelease object
auto scene = SplashScene::createScene();

// run
director->runWithScene(scene);

return true;

标签: c++cocos2d-x

解决方案


#Botje 给我答案:

在我的 Scene* SplashScene::createScene() 实现中,我替换了

auto layer = Scene::create();

经过

auto layer = SplashScene::create();

推荐阅读