Cocos2d-x LayoutComponent的使用

本文介绍了在Cocos2d-x中如何使用LayoutComponent进行UI布局管理。通过创建不同位置的精灵并应用LayoutComponent,设置水平和垂直边缘,确保元素正确对齐。同时,展示了触摸事件的监听和响应,调整LayerColor大小以适应屏幕尺寸。
摘要由CSDN通过智能技术生成

void touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
cocos2d::LayerColor* _baseLayer;
void HelloWorld::init2();

------------------------------------------------------------------------------------------------------------

Size widgetSize = Director::getInstance()->getVisibleSize();
_baseLayer = LayerColor::create();
_baseLayer->setColor(Color3B(50, 100, 0));
_baseLayer->setOpacity(100);
_baseLayer->setContentSize(Size(200, 200));
this->addChild(_baseLayer);

Button* button = Button::create("animationbuttonnormal.png");
CCLOG("content size should be greater than 0:  width = %f, height = %f", button->getContentSize().width,
button->getContentSize().height);
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this));
button->setZoomScale(0.4f);
button->setPressedActionEnabled(true);
this->addChild(button);


void HelloWorld::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
break;

case Widget::TouchEventType::MOVED:
break;


case Widget::TouchEventType::ENDED:
{
Size widgetSize = Director::getInstance()->getVisibleSize();
Size layerSize = Size(300,200);
if (layerSize.width == widgetSize.width && layerSize.height == widgetSize.height)
_baseLayer->setContentSize(Size(200, 200));
else
_baseLayer->setContentSize(widgetSize);
//ui:Helper::doLayout(_baseLayer);
init2();
}
break;


case Widget::TouchEventType::CANCELED:
break;


default:
break;
}
}


void HelloWorld::init2()
{
Sprite* leftTopSprite = Sprite::create("CloseSelected.png");
LayoutComponent* leftTop = LayoutComponent::bindLayoutComponent(leftTopSprite);
leftTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
_baseLayer->addChild(leftTopSprite);


Sprite* leftBottomSprite = Sprite::create("CloseSelected.png");
LayoutComponent* leftBottom = LayoutComponent::bindLayoutComponent(leftBottomSprite);
leftBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Left);
leftBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
_baseLayer->addChild(leftBottomSprite);


Sprite* rightTopSprite = Sprite::create("CloseSelected.png");
LayoutComponent* rightTop = LayoutComponent::bindLayoutComponent(rightTopSprite);
rightTop->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightTop->setVerticalEdge(LayoutComponent::VerticalEdge::Top);
_baseLayer->addChild(rightTopSprite);


Sprite* rightBottomSprite = Sprite::create("CloseSelected.png");
LayoutComponent* rightBottom = LayoutComponent::bindLayoutComponent(rightBottomSprite);
rightBottom->setHorizontalEdge(LayoutComponent::HorizontalEdge::Right);
rightBottom->setVerticalEdge(LayoutComponent::VerticalEdge::Bottom);
_baseLayer->addChild(rightBottomSprite);


ui::Helper::doLayout(_baseLayer);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值