سؤال

هنا يوجد مثال مع التخطيط النسبي الذي يضع 9 أزرار: http://www.cocos2d-x.org/wiki/Containers

لقد فعلت شيئا مشابها جدا لمثال ويكي مع كوكوس 2د-س 3.1 واختباره على الروبوت ويندوز.لقد صنفت من الباطن Layer وأضاف الرمز أدناه في init() الطريقة:

Layout* layout = Layout::create();
layout->setLayoutType(LayoutType::RELATIVE);
layout->setContentSize(visibleSize);
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GREEN);
// Size backgroundSize = background->getContentSize();
layout->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2));
/*_uiLayer->*/addChild(layout);

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

RelativeLayoutParameter* rp_RightBottom = RelativeLayoutParameter::create();
rp_RightBottom->setAlign(RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM);
button_RightBottom->setLayoutParameter(rp_RightBottom); 

المرفقة هي نتيجة لهذا الرمز.تستطيع أن ترى 9 أزرار فقاعة حمراء.توجد هذه الأزرار في وسط شاشتي ولكني كنت أتوقع رؤيتها في أعلى اليمين, أعلى الوسط,.....وجميع 9 أماكن أخرى على الشاشة.لماذا هم قريبون جدا من بعضهم البعض?

enter image description here

كما أنني لا أرى تخطيطات بلون أخضر خالص.ما هو الخطأ?

هل كانت مفيدة؟

المحلول

تحتاج إلى تغيير layout->setContentSize(visibleSize); إلى layout->setSize(visibleSize);.

أيضا لتصحيح تخطيط مع الألوان القيام بما يلي:

layout->setBackGroundColor(Color3B::GREEN);
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top