图层不会绘制另一个图层

Layer doesn't draw in another one

本文关键字:图层 另一个 绘制      更新时间:2023-10-16

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

this->addChild(layer);

但实际上什么都没有出现。我已经通过音频引擎添加了音乐并播放,但我仍然在主场景中看不到任何东西。 Cocos2d-x版本是3.16(最新),我也在win32上使用最新的MSVC。

您需要在场景中添加一次图层作为子图层,然后在将精灵或其他节点添加到图层之后。例如创建从 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);
//...