[Bug] CCMenuItemSprite needs to update sprite content size.

[Bug] CCMenuItemSprite needs to update sprite content size.
0.0 0


Problem Codes:

CCSprite * pNormalSprite= CCSprite::spriteWithFile(FILENAME);
CCSprite * pSelectedlSprite= CCSprite::spriteWithFile(FILENAME);
CCMenuItemSprite * item = CCMenuItemSprite::itemFromNormalSprite(pNormalSprite, pSelectedlSprite);
CCMenu *menu = CCMenu::menuWithItems;


in CCMenuItemSprite.cpp Line 385

in function
bool CCMenuItemSprite::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, SelectorProtocol* target, SEL_MenuHandler selector)

m_pNormalImage->getContentSize() cannot get the scaled content size. It results in getting the wrong size of the menu button.
This line could be


nope. CCMenuItemSprite inherits from CCNode, has its own ContentSize, ContentSizeInPixels & boudingBox. We can’t mix up them, simply equal its contentSize to boundingBox.
Can you paste a screenshot to describe the your problem?


Actually the problem is not only in that sentence. Cocos2d-x uses CCMenuItem::rect() to calculate the menu size and the touch zone. CCMenuItem::rect() simply take the width and heighth of content size which initialled from here : this->setContentSize(m_pNormalImage->getContentSize()); This line only takes the current normal sprite’s original size (it has not been added and transformed into scene so the scale factor cannot be effective here, in my opinion).

I am not sure if the content size of the original sprite should remain the same when it scales. If so I think there should be another size to calculate rect().

In this example I uploaded, I scaled “Play” and other sprites to 50. The menu item treated the size differently so the touch zone is 200 bigger than it should be. Also there is an offset in position.


In my opinion, the content size means the original size of sprite. It wouldn’t be change in normal case.

If you want scale the menu item size, can do like this:

    // add close menu
    CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
    CCMenu* pMenu =CCMenu::menuWithItems(pCloseItem, NULL);


WenSheng Yang wrote:

In my opinion, the content size means the original size of sprite. It wouldn’t be change in normal case.
If you want scale the menu item size, can do like this:

Yes, it works. Thank you. However, I want to use one set of texture image for both low and high screen resolutions. Since I work with managed sprite, the sprite manager scale it based on the current resolution when created.
If the content size remains unchanged, to scale the CCMenuItemImage should not work for its touch zone. Since rect() uses the content size only.

I found another bug. It seems there is a difference between CCMenuItemImage and CCMenuItemSprite.

Try this code, it won’t work.
@ CCSprite * sprite1 = CCSprite::spriteWithFile(“CloseNormal.png”);
CCSprite * sprite2 = CCSprite::spriteWithFile(“CloseSelected.png”);
CCMenuItemSprite * pCloseItem = CCMenuItemSprite::itemFromNormalSprite(sprite1, sprite2, this, menu_selector(HelloWorld::menuCloseCallback));
pCloseItem~~>setPosition~~>getWinSize.width~~ 20, 20) );
CCActionInterval * alphaaction = CCFadeIn::actionWithDuration;

However, the fadein works fine in original CCMenuItemImage pCloseItem.

P.S. One more. Run the push scene test and quit without pop that scene will result in crash.


Thanks very much.
We reproduct the bug: CCMenuItemSprite can’t run action with FadeIn.
There is a issue #499 for it, we’ll fix it in next version.


@h5nc Thor
issue #499 is done. Thanks for your report. You can get the edge version from github and have a test.


Thx. Just tested with my project. Works perfect.