Cocos2d-x Layout使用三

本文详细介绍了如何使用Cocos2d-x创建一个相对布局,并添加按钮到布局的不同位置,如顶部左中右、底部左中右等,展示了RelativeLayoutParameter在调整元素位置中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Layout* layout = Layout::create();
layout->setLayoutType(LayoutType::RELATIVE);
layout->setContentSize(Size(600, 550));
layout->setAnchorPoint(Point(0.5f,0.5f));
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GREEN);
Size size = Director::getInstance()->getVisibleSize();
layout->setPosition(Vec2(size.width  / 2.0f ,size.height / 2));
this->addChild(layout);

// top left
Button* button_TopLeft = Button::create("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.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("animationbuttonnormal.png","animationbuttonpressed.png");
layout->addChild(button_RightBottom);

RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create();
rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM);
button_RightBottom->setLayoutParameter(rp_RightBottom);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值