fixed #1547:set selected menu item to null when it is removed from a menu

This commit is contained in:
minggo 2013-01-17 15:04:48 +08:00
parent cc2865e294
commit a7dca5c05a
4 changed files with 101 additions and 4 deletions

View File

@ -178,15 +178,32 @@ void CCMenu::addChild(CCNode * child, int zOrder, int tag)
void CCMenu::onExit()
{
if (m_eState == kCCMenuStateTrackingTouch)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
m_eState = kCCMenuStateWaiting;
m_pSelectedItem = NULL;
}
m_eState = kCCMenuStateWaiting;
}
CCLayer::onExit();
}
void CCMenu::removeChild(CCNode* child, bool cleanup)
{
CCMenuItem *pMenuItem = dynamic_cast<CCMenuItem*>(child);
CCAssert(pMenuItem != NULL, "Menu only supports MenuItem objects as children");
if (m_pSelectedItem == pMenuItem)
{
m_pSelectedItem = NULL;
}
CCNode::removeChild(child, cleanup);
}
//Menu - Events
void CCMenu::setHandlerPriority(int newPriority)

View File

@ -125,6 +125,7 @@ public:
virtual void addChild(CCNode * child, int zOrder);
virtual void addChild(CCNode * child, int zOrder, int tag);
virtual void registerWithTouchDispatcher();
virtual void removeChild(CCNode* child, bool cleanup);
/**
@brief For phone event handle functions

View File

@ -68,12 +68,15 @@ MenuLayerMainMenu::MenuLayerMainMenu()
// Font Item
CCMenuItemFont* item8 = CCMenuItemFont::create("Quit", this, menu_selector(MenuLayerMainMenu::onQuit));
CCMenuItemFont* item9 = CCMenuItemFont::create("Remove menu item when moving", this,
menu_selector(MenuLayerMainMenu::menuMovingCallback));
CCActionInterval* color_action = CCTintBy::create(0.5f, 0, -255, -255);
CCActionInterval* color_back = color_action->reverse();
CCSequence* seq = CCSequence::create(color_action, color_back, NULL);
item8->runAction(CCRepeatForever::create(seq));
CCMenu* menu = CCMenu::create( item1, item2, item3, item4, item5, item6, item7, item8, NULL);
CCMenu* menu = CCMenu::create( item1, item2, item3, item4, item5, item6, item7, item8, item9, NULL);
menu->alignItemsVertically();
@ -185,6 +188,11 @@ void MenuLayerMainMenu::onQuit(CCObject* sender)
//getCocosApp()->exit();
}
void MenuLayerMainMenu::menuMovingCallback(CCObject *pSender)
{
((CCLayerMultiplex*)m_pParent)->switchTo(6);
}
//------------------------------------------------------------------
//
// MenuLayer2
@ -571,6 +579,58 @@ void BugsTest::backMenuCallback(cocos2d::CCObject *pSender)
((CCLayerMultiplex*)m_pParent)->switchTo(0);
}
RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* label = CCLabelTTF::create("click item and move, should not crash", "Arial", 20);
label->setPosition(ccp(s.width/2, s.height - 30));
addChild(label);
item = CCMenuItemFont::create("item 1");
item->retain();
CCMenuItemFont *back = CCMenuItemFont::create("go back", this, menu_selector(RemoveMenuItemWhenMove::goBack));
CCMenu *menu = CCMenu::create(item, back, NULL);
addChild(menu);
menu->alignItemsVertically();
menu->setPosition(ccp(s.width/2, s.height/2));
setTouchEnabled(true);
}
void RemoveMenuItemWhenMove::goBack(CCObject *pSender)
{
((CCLayerMultiplex*)m_pParent)->switchTo(0);
}
RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove()
{
CC_SAFE_RELEASE(item);
}
void RemoveMenuItemWhenMove::registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -129, false);
}
bool RemoveMenuItemWhenMove::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
return true;
}
void RemoveMenuItemWhenMove::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
if (item)
{
item->removeFromParentAndCleanup(true);
item->release();
item = NULL;
}
}
void MenuTestScene::runThisTest()
{
CCLayer* pLayer1 = new MenuLayerMainMenu();
@ -579,8 +639,9 @@ void MenuTestScene::runThisTest()
CCLayer* pLayer4 = new MenuLayer4();
CCLayer* pLayer5 = new MenuLayerPriorityTest();
CCLayer* pLayer6 = new BugsTest();
CCLayer* pLayer7 = new RemoveMenuItemWhenMove();
CCLayerMultiplex* layer = CCLayerMultiplex::create(pLayer1, pLayer2, pLayer3, pLayer4, pLayer5, pLayer6, NULL);
CCLayerMultiplex* layer = CCLayerMultiplex::create(pLayer1, pLayer2, pLayer3, pLayer4, pLayer5, pLayer6, pLayer7, NULL);
addChild(layer, 0);
pLayer1->release();
@ -589,6 +650,7 @@ void MenuTestScene::runThisTest()
pLayer4->release();
pLayer5->release();
pLayer6->release();
pLayer7->release();
CCDirector::sharedDirector()->replaceScene(this);
}

View File

@ -28,6 +28,7 @@ public:
void menuCallbackPriorityTest(CCObject* pSender);
void menuCallbackBugsTest(CCObject *pSender);
void onQuit(CCObject* pSender);
void menuMovingCallback(CCObject *pSender);
//CREATE_NODE(MenuLayer1);
};
@ -109,6 +110,22 @@ public:
void backMenuCallback(CCObject *pSender);
};
class RemoveMenuItemWhenMove : public CCLayer
{
public:
RemoveMenuItemWhenMove();
~RemoveMenuItemWhenMove();
virtual void registerWithTouchDispatcher(void);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void goBack(CCObject *pSender);
private:
CCMenuItemFont *item;
};
class MenuTestScene : public TestScene
{