加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x 布局Layout , VBox,HBox可以互相嵌套

发布时间:2020-12-14 16:52:47 所属栏目:百科 来源:网络整理
导读:?? Size winSize = Director::getInstance()-getVisibleSize(); Layout* _verticalLayout = cocos2d::ui::VBox::create();//水平布局 _verticalLayout-setPosition(Vec2(winSize.width / 2 - 80,winSize.height - 70)); Layer* _uiLayer = Layer::create();
??

Size winSize = Director::getInstance()->getVisibleSize();
Layout* _verticalLayout = cocos2d::ui::VBox::create();//水平布局
_verticalLayout->setPosition(Vec2(winSize.width / 2 - 80,winSize.height - 70));
Layer* _uiLayer = Layer::create();
_uiLayer->addChild(_verticalLayout);
_verticalLayout->setScale(0.5);

_verticalLayout->setFocused(true);
_verticalLayout->setLoopFocus(true);
_verticalLayout->setTag(100);
//_firstFocusedWidget = _verticalLayout;

int count1 = 1;
for (int i = 0; i<count1; ++i) {
ImageView *w = ImageView::create("scrollviewbg.png");
w->setAnchorPoint(Vec2::ZERO);
w->setTouchEnabled(true);
w->setScaleX(2.5);
w->setTag(i + count1);
w->addTouchEventListener(CC_CALLBACK_2(HelloWorld::onImageViewClicked,this));
_verticalLayout->addChild(w);
}

//add HBox into VBox
HBox *hbox = HBox::create();//垂直布局
hbox->setScale(0.8f);
hbox->setTag(101);
_verticalLayout->addChild(hbox);

int count2 = 2;
for (int i = 0; i < count2; ++i) {
ImageView *w = ImageView::create("scrollviewbg.png");
w->setAnchorPoint(Vec2(0,1));
w->setScaleY(2.0);
w->setTouchEnabled(true);
w->setTag(i + count1 + count2);
w->addTouchEventListener(CC_CALLBACK_2(HelloWorld::onImageViewClicked,this));
hbox->addChild(w);
}

VBox *innerVBox = VBox::create();
hbox->addChild(innerVBox);//水平布局中加入垂直布局
innerVBox->setTag(102);
// innerVBox->setPassFocusToChild(false);
// innerVBox->setFocusEnabled(false);


int count3 = 2;
for (int i = 0; i<count3; ++i) {
ImageView *w = ImageView::create("scrollviewbg.png");
w->setTouchEnabled(true);
w->setTag(i + count1 + count2 + count3);
w->addTouchEventListener(CC_CALLBACK_2(HelloWorld::onImageViewClicked,this));
innerVBox->addChild(w);
}

this->addChild(_verticalLayout);


return true;
}

void HelloWorld::onImageViewClicked(cocos2d::Ref *ref,cocos2d::ui::Widget::TouchEventType touchType) { if (touchType == cocos2d::ui::Widget::TouchEventType::ENDED) { cocos2d::ui::Widget *w = (cocos2d::ui::Widget*)ref; if (w->isFocusEnabled()) { w->setFocusEnabled(false); w->setColor(Color3B::YELLOW); } else{ w->setFocusEnabled(true); w->setColor(Color3B::WHITE); } } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读