Looking over the documentation, it says that cc.LabelBMFont can be “can be used as part of a menu item”. While I have been able to instantiate a cc.MenuItemLabel with a cc.LabelBMFont, it doesn’t act like a menu item, meaning my callback is not triggered when the menu item is clicked. Basically, it acts just like a label that was added to a regular cc.Node, instead of an actual menu item.
Here’s a snippet of the code I’m using to instantiate the LabelBMFont as well as the MenuItemLabel:
var label = new cc.LabelBMFont("TestLabel", res.Viga12pt);
var menuItemLabel = new cc.MenuItemLabel(label, this.onTestLabelPressed, this);
menuItemLabel.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
this.menu.addChild(menuItemLabel);
While this won’t help much, here’s a screenshot of the menu item label. Clicking on it does absolutely nothing (no scale up/down effect on mouse press/release, and the menu item’s callback is never triggered)
Seriously, any help would be appreciated. I’m at my wit’s end trying to figure out what’s wrong here.
Okay, so I believe I’ve found a bug in cocos2d-js, related to using cc.LabelBMFont with cc.MenuItemLabels. It appears as though loading a LabelBMFont is done asynchronously, so at the time it is added to a cc.MenuItemLabels its content size is [0, 0] (this makes sense, the font file hasn’t been loaded yet).
When the font file is eventually loaded and the cc.LabelBMFont is properly initialized, its content size is then properly updated. However, the cc.MenuItemLabel’s content size never gets updated. This causes a few errors with respect to targeting the node on a touch (can’t do it in cc.Menu._itemForTouch since its size is [0, 0], the menu item can’t be positioned correctly if the anchor point is anything other than (0, 0), etc …).
I put together a crappy fix for this, by placing these lines at the bottom of cc.LabelBMFont::createFontChars:
if (this.parent) {
this.parent.setContentSize(this.getContentSize());
}
However, that’s clearly a terrible way to fix this problem. I’m assuming that a proper fix would somehow include overriding cc.MenuItem::rect() in cc.MenuItemLabel with code that calculates the rect’s content sized based off of the child label, however I don’t believe that will fix the problems that come from trying to position the menu item with an anchor point that is not (0, 0).
I can get by with my crappy hack, but if anybody has a better suggestion on how to properly fix this issue properly I’m all ears.