Question

How can I reference a menu item position to another menu item in the same menu?

For example, I have the menu object and 2 menu items. I want the second menu item, refers to the position of the first, like this:

menu_item_2->setPosition(Point(menu_item1.width/2, visibleSize.height/3));

But this doesn't actually work.

This is my code now:

Size visibleSize = Director::getInstance()->getVisibleSize();



    auto menu_item_1 = MenuItemFont::create("Play", CC_CALLBACK_1(MenuScene::Play, this));
    auto menu_item_2 = MenuItemFont::create("abc", CC_CALLBACK_1(MenuScene::Play, this));
    mainMenu = Menu::create(menu_item_1,menu_item_2, NULL);
    mainMenu -> setPosition(visibleSize.width/4, visibleSize.height/4);
    menu_item_1->setPosition(Point(visibleSize.width/2, visibleSize.height/2));
    menu_item_2->setPosition(Point(visibleSize.width/2, visibleSize.height/3));
    this->addChild(mainMenu);


    CCLOG("Play");
    CCLOG("Play %f", screen.width/2);
    CCLOG("Play %f", screen.height/2);
     CCLOG("Play %f", visibleSize.width/2);
    CCLOG("Play %f", visibleSize.height/2);
Was it helpful?

Solution

You can refer to a width property of another item menu by accessing his bounding box

menu_item_2->setPosition(Point(menu_item_1->getBoundingBox().size.width/2.0f, m_visibleSize.height/3));

Essentially a MenuItemFont is a Node class and then you can use all getters present in a node class:

Point pos = menu_item_1->getPosition();
float xpos = menu_item_1->getPositionX();
float ypos = menu_item_1->getPositionY();

But be careful : all these coordinates are relative to main menu object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top