Cannot layout a HBox

Cannot layout a HBox
0

#1

Hello

I cannot place horizaonal layout correctly
Code from Scene init method

         auto ui = cocos2d::Layer::create();
 	addChild(ui);
 
 	auto hbox = cocos2d::ui::HBox::create();
 	hbox->setPosition(cocos2d::Vec2::ZERO);
 	hbox->setContentSize(cocos2d::Size(101 * 10, 81 * 7));
 	
 	auto map = cocos2d::TMXTiledMap::create("level.tmx");
 	hbox->addChild(map);
 
 	map = cocos2d::TMXTiledMap::create("rect.tmx");
 	hbox->addChild(map);

But it shows like this


Rect.tmx placed upon a level.tmx. But I expected it on the right side of level (empty red rectangle on image)


#2

You have told it 0,0 which is the lower left corner.


#3

Thank you for reply. I thought I set lower left position to hbox only. I supposed that hbox children placed in it in horizontal row. Or I should set position of each hbox child manually?


#4

Sorry, you want the box on the left with the red border moved to the right box with the red border?


#5

I want to place rect.tmx in a horizontal row, right of level.tmx (without manually setPosition of each child). Like this

I supposed that hbox is right container for this


#6

ok, I see why you are using HBox now. To layout the in a horizontal line. I don’t use it.

You know the size and position of level so you can just place rect at levels width,0?


#7

Using HBox is equally to use Layout with setLayoutType, isnt’t?

        Layout* layout = Layout::create();
        layout->setLayoutType(Layout::Type::HORIZONTAL);

Yes, I know the size and position of level. But isnt’t HBox used for automatic placing?


#8

I’m not sure how often other developers use it. I never use it or layouts for that matter. I just always write my own calculations.

That being said, I can test HBox and see what is happening.


#9

HBox is only works with Widget-derived classes. (see UILayout.h comments).


#10

That explains it.