CCScorllLayerButton is so cool by leo.
so I make a new class. but that has a problem.
it is for CCMenuItemSprite, but it doesn’t work when I add child to CCMenu.
doesn’t work means “parent Node was not move when I clicked and drag button.”
Why?
if you just addchild to this ( like Layer ), it was work.
but I want to use CCMenu’s alignVertically fuction also.
is it possible?
// by Korean
버튼을 끌면, 스크롤되는 버튼을 만들고 있습니다.
그런데 CCMenu에 addChild를 하면 버튼이 제 기능을 하지 못합니다.
왜 그럴까요? 이것을 고칠 수 있을까요?
/////////////////////////////////////////// using example ///////////////////////////////////////////////////////
CCsize btn_size = CCSize ( 100, 20 );
CCProgress* btn_nor = CCProgress::progressWithFile (sprite_1.png, size_btn );
CCProgress* btn_sel = CCProgress::progressWithFile ( sprite_2.png, size_btn );
CCProgress* btn_dis = CCProgress::progressWithFile ( sprite_3.png, size_btn );
CCLayerPopupScrollByButtonMenuItem* btn = CCLayerPopupScrollByButtonMenuItem::buttonFromNormalSprite ( btn_nor, btn_sel, btn_dis, this, menu_selector ( HBLayerPopupScrollPage::onTouch ) );
{
//bla bla bla......
}
this->addChild ( btn );
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CCLayerPopupScrollByButtonMenuItem : public CCMenuItemSprite, CCTargetedTouchDelegate
{
protected :
CCLayerPopupScrollByButtonMenuItem ( KDvoid );
~CCLayerPopupScrollByButtonMenuItem ( KDvoid );
private :
KDbool initFromNormalSprite ( CCNode* normal, CCNode* selected, CCNode* disabled, SelectorProtocol* target, SEL_MenuHandler selector );
virtual KDvoid onEnter ( KDvoid );
virtual KDvoid onExit ( KDvoid );
CCRect rect( KDvoid );
KDbool containsTouchLocation ( CCTouch* touch );
virtual KDbool ccTouchBegan ( CCTouch* pTouch, CCEvent* pEvent );
virtual KDvoid ccTouchMoved ( CCTouch* pTouch, CCEvent* pEvent );
virtual KDvoid ccTouchEnded ( CCTouch* pTouch, CCEvent* pEvent );
SelectorProtocol* m_target;
SEL_MenuHandler m_selector;
KDbool m_touched;
CCPoint m_BeginPos;
CCPoint m_CurPos;
public :
static CCLayerPopupScrollByButtonMenuItem* buttonFromNormalSprite ( CCNode* normal, CCNode* selected, CCNode* disabled, SelectorProtocol* target, SEL_MenuHandler selector );
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
CCLayerPopupScrollByButtonMenuItem::CCLayerPopupScrollByButtonMenuItem ( KDvoid )
{
}
CCLayerPopupScrollByButtonMenuItem::~CCLayerPopupScrollByButtonMenuItem ( KDvoid )
{
}
KDbool CCLayerPopupScrollByButtonMenuItem::initFromNormalSprite ( CCNode* normal, CCNode* selected, CCNode* disabled, SelectorProtocol* target, SEL_MenuHandler selector )
{
KDbool init = CCMenuItemSprite::initFromNormalSprite ( normal, selected, disabled, target, selector );
if ( !init )
{
return KD_FALSE;
}
this->m_target = target;
this->m_selector = selector;
this->m_touched = KD_FALSE;
return KD_TRUE;
}
KDvoid CCLayerPopupScrollByButtonMenuItem::onEnter ( KDvoid )
{
CCTouchDispatcher::sharedDispatcher ( )->addTargetedDelegate ( this, 0, false );
CCMenuItemSprite::onEnter ( );
}
KDvoid CCLayerPopupScrollByButtonMenuItem::onExit ( KDvoid )
{
CCTouchDispatcher::sharedDispatcher ( )->removeDelegate ( this );
CCMenuItemSprite::onExit ( );
}
CCRect CCLayerPopupScrollByButtonMenuItem::rect ( KDvoid )
{
CCSize s = this->getNormalImage ( ) ->getContentSize ( );
return CCRectMake ( -s.cx / 2, -s.cy / 2, s.cx, s.cy );
}
KDbool CCLayerPopupScrollByButtonMenuItem::containsTouchLocation ( CCTouch* touch )
{
return CCRect::CCRectContainsPoint ( rect ( ), this->convertTouchToNodeSpaceAR ( touch ) );
}
KDbool CCLayerPopupScrollByButtonMenuItem::ccTouchBegan ( CCTouch* pTouch, CCEvent* pEvent)
{
if ( containsTouchLocation ( pTouch ) )
{
m_touched = KD_TRUE;
m_BeginPos = pTouch->locationInView ( pTouch->view ( ) );
return KD_TRUE;
}
return KD_FALSE;
}
KDvoid CCLayerPopupScrollByButtonMenuItem::ccTouchMoved ( CCTouch* pTouch, CCEvent* pEvent )
{
CCPoint m_CurPos = pTouch->locationInView ( pTouch->view ( ) );
//
if ( ccpDistance ( m_BeginPos, m_CurPos ) > TAP_MAX_DRAG )
{
m_touched = KD_FALSE;
}
if ( !CCPoint::CCPointEqualToPoint ( m_BeginPos, m_CurPos ) )
{
m_touched = KD_FALSE;
}
KDfloat diffPosY = m_CurPos.y - m_BeginPos.y;
// that is so funny, but it was work if you want controll any parent node. haha...
CCPoint curPos = this->getParent ( )->getParent ( )->getParent ( )->getParent ( )->getPosition ( );
CCPoint nextPos = ccp ( curPos.x, curPos.y + diffPosY );
this->getParent ( )->getParent ( )->getParent ( )->getParent ( )->setPosition ( nextPos );
m_BeginPos = m_CurPos;
}
KDvoid CCLayerPopupScrollByButtonMenuItem::ccTouchEnded ( CCTouch* pTouch, CCEvent* pEvent )
{
if ( m_touched )
{
if ( m_target != KD_NULL && m_selector != KD_NULL )
{
( m_target->*m_selector ) ( this );
}
}
else if ( m_touched == KD_FALSE )
{
}
}
CCLayerPopupScrollByButtonMenuItem* CCLayerPopupScrollByButtonMenuItem::buttonFromNormalSprite ( CCNode* normal, CCNode* selected, CCNode* disabled, SelectorProtocol* target, SEL_MenuHandler selector )
{
CCLayerPopupScrollByButtonMenuItem *pRet = new CCLayerPopupScrollByButtonMenuItem ( );
if ( pRet && pRet->initFromNormalSprite ( normal, selected, disabled, target, selector ) )
{
pRet->autorelease ( );
return pRet;
}
CC_SAFE_DELETE ( pRet )
return KD_NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////