【问题标题】:Layer doesn't draw in another one图层不绘制另一个
【发布时间】:2018-05-05 15:53:12
【问题描述】:

我正在尝试制作类似于自定义消息框的东西,但 MB 层不想显示在主层(实际上是场景)上。我已经用 2 个文本框实现了非常基本的层(cocos2d::Layer)。我通过

将它添加到场景中
this->addChild(layer);

但实际上什么也没显示。我已经通过 AudioEngine 添加了音乐并且它可以播放,但我仍然在主要场景中什么也看不到。 Cocos2d-x 版本是 3.16(最新),我在 win32 上使用的是最新的 MSVC。

【问题讨论】:

  • 1) minimal reproducible example 请。 2)这是什么图书馆? 3)这是什么平台? 4)你使用什么编译器? 5) 你编译的 C++ 标准是什么?简而言之;不要让我们猜测。在问题中提供所有相关详细信息。
  • @JesperJuhl 2) cocos2d-x 3.16 3) win32 4) MSVC 2017 5) 最新
  • 谢谢。现在请编辑您的问题并将信息放在那里(从一开始就应该放在哪里)而不是在评论中。 (哦,顺便说一句,您忘记了“1”——最重要的一个)。

标签: c++ drawing cocos2d-x


【解决方案1】:

您需要在场景中添加层作为子层一次,然后将精灵或其他节点添加到您的层中。例如创建继承自 cocos2d::Layer 的测试场景 HelloWorld:

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
//...

然后使用工厂方法将场景创建定义为:

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

在 init 方法中尝试将您的简单对象添加到继承自 cocos2d::Layer 的 HelloWorld,并且 Layer 是 cocos2d::Scene 的子对象:

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

    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);

在所有这些步骤从导演那里运行这个场景之后:

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

// run
director->runWithScene(scene);
//...

【讨论】:

  • 我已经有一个带图层的场景,但我想在场景中再添加一个图层
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多