Custom MenuItem Hit Area

Hi, I’m struggling to figure out how to control the hit area of my MenuItems. The code below shows what I’m doing:

    // a Node subclass containing a Slice9Sprite background and a Label:
    playButtonView = BoxButton::create("PLAY", 50); 
   
    playButton = cocos2d::MenuItemSprite::create(
        playButtonView,
        playButtonView,
        CC_CALLBACK_1(DevPregameScreen::onClick, this));

    menu = cocos2d::Menu::create(
        playButton,
        NULL);

The BoxButton is laid out centered on the [0,0] point (by which I mean, the anchorPoint is int he middle of the button.) Internally, it uses:

    this->setContentSize()

My problem is that the hit area of the button is offset, so I can only click on the upper right quarter of the button. It is obvious that the hit area is a rectangle that starts at [0,0], rather than the bounding box of the button. Is there any way to offset / control the hit area? Do all menu Items have to be designed around this restriction? It makes it a lot more difficult to calculate positions and offsets when dynamically laying out content.

Thanks :smile:

are you doing this because you have a single image that you are trying to create menu items out of without breaking up the image?

Hi @slackmoehrle. No, the BoxButton is just a view consisting of a Slice9Sprite background, and a Label. It has come methods for resizing, setting the label, etc. I suppose I could inherit directly from MenuItem, rather than using a MenuItemSprite. I have taken this approach elsewhere, and had the same problem with hit areas having the origin at [0, 0] rather than being the bounding box of the object.

Unless I’m misunderstanding something it feels a bit like a bug / missing feature for the menu to ignore the anchor point of the button sprite. It doesn’t seem to me like I’m doing anything really weird or using the api not as it is intended.