When using CCMenuItemToggle, the initWithTargetMethod() calls setSelectedIndex(0) by default.
The CCMenuItemToggle::setSelectedIndex() method looks like this:
void CCMenuItemToggle::setSelectedIndex(unsigned int index)
{
if( index != m_uSelectedIndex )
{
m_uSelectedIndex = index;
this->removeChildByTag(kCurrentItem, false);
CCMenuItem *item = m_pSubItems->getObjectAtIndex(m_uSelectedIndex);
this->addChild(item, 0, kCurrentItem);
const CCSize& s = item->getContentSize();
this->setContentSize(s);
item->setPosition( ccp( s.width/2, s.height/2 ) );
}
}
The call to removeChildByTag will always fail on the first instance because no current item has ever been selected.
If the code is changed to:
void CCMenuItemToggle::setSelectedIndex(unsigned int index)
{
if( index != m_uSelectedIndex )
{
if ( m_uSelectedIndex!=UINT_MAX )
{
this->removeChildByTag(kCurrentItem, false);
}
m_uSelectedIndex = index;
CCMenuItem *item = m_pSubItems->getObjectAtIndex(m_uSelectedIndex);
this->addChild(item, 0, kCurrentItem);
const CCSize& s = item->getContentSize();
this->setContentSize(s);
item->setPosition( ccp( s.width/2, s.height/2 ) );
}
}
The unnecessary call to removeChildByTag isn’t made.
Roc