尝试使用 Cocos2D-x C++ 调用初始化函数

Posted

技术标签:

【中文标题】尝试使用 Cocos2D-x C++ 调用初始化函数【英文标题】:Trying to call an init function with Cocos2D-x C++ 【发布时间】:2021-03-06 06:46:02 【问题描述】:

我正在尝试复制这个教程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;

【问题讨论】:

代码的哪一部分应该调用init 您实际上在哪里创建了SplashScene 对象?我只看到两个Scene 对象。大概你打算写layer = SplashScene::create(); #UnholySheep 我不知道,在教程中,我没有看到该函数是否被手动调用:/ 我尝试了“scene->init();”,但它什么也没做# Botje 是的,我没看到!谢谢 ! :) 我投票决定以拼写错误结束,因为这个问题最终对未来的读者没有用,除非那些碰巧在同一个 YouTube 教程上犯了同样的拼写错误。 【参考方案1】:

#Botje 给我答案:

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

auto layer = Scene::create();

通过

auto layer = SplashScene::create();

【讨论】:

以上是关于尝试使用 Cocos2D-x C++ 调用初始化函数的主要内容,如果未能解决你的问题,请参考以下文章

C++调用JS,可变參模板实现,方便调用(cocos2d-x)

cocos2d-x斜线时隐藏状态栏

Cocos2d-x场景生命周期函数介绍

Sprite::create(“file.png”) 在 Cocos2d-x C++ Visual Studio 上返回 null

Cocos2d-x使用iOS游戏内付费IAP(C++篇)

cocos2d-x实战 C++卷 学习笔记--第6章 场景与层