Cocos2d-x 布局Layout , VBox,HBox可以互相嵌套
2016-01-28 23:15
477 查看
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);
}
}
}
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);
}
}
}
相关文章推荐
- cocos-js开发html5游戏视频教程
- Cocos2d-x EditBox 带有监听功能
- cocos KeyBoard
- cocos中ProgressTimer,进度条
- cocos2dx socket 通信
- 如何优化cocos2d程序的内存使用和程序大小:第一部分
- 【cocos2dx】卡牌战斗_表现层实现框架
- 【cocos2dx】状态机应用场景及原理
- cocos2d-js获得其它Layer层对象
- Cocos2d-x 3.0final手机游戏开发视频教程2014 - 自学编程 -(陆续更新中)
- ndk-stack 调试NDK程序
- 【cocos3.x+box2d+tileMap】制作马里奥游戏(四)碰撞检测
- Cocos2d-x 原生EditBox三种形式的使用
- cocos2dx 3.3 AssetsManager简单分析
- cocos studio 发布资源的问题
- cocos的json资源的相对目录和绝对目录
- cocos的plist最好不用
- Cocos2d学习笔记2——cocos命令行指令使用
- 23、Cocos2dx 3.0游戏开发找小三之粒子系统:你那里下雪了吗?
- Cocos2d Android项目手动编译日记之SDK版本(一)