gpt4 book ai didi

c++ - 相对布局不适用于 cocos2d-x 3.1?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:49 33 4
gpt4 key购买 nike

这里有一个放置 9 个按钮的相对布局示例:http://www.cocos2d-x.org/wiki/Containers

我用 cocos2d-x 3.1 做了一些与 wiki 示例非常相似的事情,并在 Android 和 Windows 上测试了它。我对 Layer 进行了子分类,并在 init() 方法中添加了以下代码:

Layout* layout = Layout::create();
layout->setLayoutType(LayoutType::RELATIVE);
layout->setContentSize(visibleSize);
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GREEN);
// Size backgroundSize = background->getContentSize();
layout->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2));
/*_uiLayer->*/addChild(layout);

// top left
Button* button_TopLeft = Button::create("images/ingame/bubbe.png");
layout->addChild(button_TopLeft);

RelativeLayoutParameter* rp_TopLeft = RelativeLayoutParameter::create();
rp_TopLeft->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT);
button_TopLeft->setLayoutParameter(rp_TopLeft);


// top center horizontal
Button* button_TopCenter = Button::create("images/ingame/bubbe.png");
layout->addChild(button_TopCenter);

RelativeLayoutParameter* rp_TopCenter = RelativeLayoutParameter::create();
rp_TopCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL);
button_TopCenter->setLayoutParameter(rp_TopCenter);


// top right
Button* button_TopRight = Button::create("images/ingame/bubbe.png");
layout->addChild(button_TopRight);

RelativeLayoutParameter* rp_TopRight = RelativeLayoutParameter::create();
rp_TopRight->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT);
button_TopRight->setLayoutParameter(rp_TopRight);


// left center
Button* button_LeftCenter = Button::create("images/ingame/bubbe.png");
layout->addChild(button_LeftCenter);

RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create();
rp_LeftCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL);
button_LeftCenter->setLayoutParameter(rp_LeftCenter);


// center
Button* buttonCenter = Button::create("images/ingame/bubbe.png");
layout->addChild(buttonCenter);

RelativeLayoutParameter* rpCenter = RelativeLayoutParameter::create();
rpCenter->setAlign(RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
buttonCenter->setLayoutParameter(rpCenter);


// right center
Button* button_RightCenter = Button::create("images/ingame/bubbe.png");
layout->addChild(button_RightCenter);

RelativeLayoutParameter* rp_RightCenter = RelativeLayoutParameter::create();
rp_RightCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL);
button_RightCenter->setLayoutParameter(rp_RightCenter);


// left bottom
Button* button_LeftBottom = Button::create("images/ingame/bubbe.png");
layout->addChild(button_LeftBottom);

RelativeLayoutParameter* rp_LeftBottom = RelativeLayoutParameter::create();
rp_LeftBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM);
button_LeftBottom->setLayoutParameter(rp_LeftBottom);


// bottom center
Button* button_BottomCenter = Button::create("images/ingame/bubbe.png");
layout->addChild(button_BottomCenter);

RelativeLayoutParameter* rp_BottomCenter = RelativeLayoutParameter::create();
rp_BottomCenter->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
button_BottomCenter->setLayoutParameter(rp_BottomCenter);


// right bottom
Button* button_RightBottom = Button::create("images/ingame/bubbe.png");
layout->addChild(button_RightBottom);

RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create();
rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM);
button_RightBottom->setLayoutParameter(rp_RightBottom);

附件是这段代码的结果。您可以看到 9 个红色气泡按钮。这个按钮位于我屏幕的中央,但我希望在右上角、中上部……以及我屏幕上的所有其他 9 个位置看到它们。为什么他们彼此如此接近?

enter image description here

我也没有看到纯绿色的布局。怎么了?

最佳答案

需要将layout->setContentSize(visibleSize);改为layout->setSize(visibleSize);

还要调试带有颜色的布局,请执行以下操作:

layout->setBackGroundColor(Color3B::GREEN);
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);

关于c++ - 相对布局不适用于 cocos2d-x 3.1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25057497/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com