How to avoid conflit between MenuItemImage & MenuItemSprite in Cocos2d-x 3.15.1

Hello,

First I’ll explain what I want to do before seeing the code, the user can click to the PLAY Button and after click, a popup Menu is displayed, the Menu contain 2 MenuItemSprite for play with bot or friend, for the MenuItemImage it’s to display a small background around 2 MenuItemSprite.

The problem is when I want to click on a MenuItemSprite inside the Menu, nothing is happening, but when I set enable the Background to false all is okay without any problem, But I don’t want this solution because I have inside another background (almost transparent). If the user click to this background, he can hide automatically the Menu, so I said set enable the background to false it’ll give another problem conflit between Background (Window) and background (Menu).

Code :

//Background (Scene)
    background=Sprite::create(BACKGROUND);
        background->setPosition(SonarCocosHelper::UI::GetScreenCenter());
        background->setOpacity(0);
        this->addChild(background,1);
    
     //Background Menu
        MenuItemImage * overlayWindowItem=MenuItemImage::create(GAME_OVER_WINDOW,GAME_OVER_WINDOW,GAME_OVER_WINDOW,NULL);
        //overlayWindowItem->setEnabled(false);
    
        //FRIEND ITEM
        MenuItemSprite * friendItem=MenuItemSprite::create(Sprite::create(FRIEND_BUTTON), Sprite::create(FRIEND_BUTTON),CC_CALLBACK_1(MenuScene::goToPlay,this));
        friendItem->setTag(PLAY_WITH_FRIEND);
        friendItem->setPosition(Vec2(-overlayWindowItem->getContentSize().width/4,friendItem->getPositionY()));

        //BOT ITEM
        MenuItemSprite * botItem=MenuItemSprite::create(Sprite::create(BOT_BUTTON), Sprite::create(BOT_BUTTON),CC_CALLBACK_1(MenuScene::goToPlay,this));
        botItem->setTag(PLAY_WITH_BOT);
        botItem->setPosition(Vec2(overlayWindowItem->getContentSize().width/4,botItem->getPositionY()));

//menu
 menu=Menu::create(overlayWindowItem,friendItem,botItem,NULL);
    menu->setPosition(Vec2(SonarCocosHelper::UI::GetScreenCenter().x,SonarCocosHelper::UI::GetScreenCenter().y+screenSize.height));
    this->addChild(menu,1);

How I can avoid conflit between MenuItemImage & MenuItemSprite, I want to click on MenuItemSprite, MenuItemImage is just a background.

Thank you,

When PLAY button is clicked, set its parent to setEnabled(false); and set to true when pop-up closed.

and overlayWindowItem should be setEnabled(false); if you want it for background, but why not creating sprite for this background?

1 Like

I took the overlayWindowItem as a background of the menu, it’ll take the animation automatically.

As you said to avoid this problem I replaced the MenuItemImage with a Sprite and I repeated the same animation of the Menu for this sprite with runAction.

Thank you,

you can also add sprite as button child, so the animation will follow as well.
MenuItem->getNormalImage()->addChild(spr1);
MenuItem->getSelectedImage()->addChild(spr2);

1 Like