我能理解的是,您正在尝试通过您的 CCMenuItemImage 进行手动控制。实际上所有的触摸都是在 CCMenu 中处理的,而不是在 MenuItem 中,所以你必须继承 CCMenu 而不是 CCMenuItemImage 来覆盖触摸。
在我的游戏中,我遇到了 CCTableView 和 CCMenuItem 的问题,其中 MenuItem 在采取手势时是优先考虑的。所以我通过继承 CCMenu 对其进行了调整。
它还包含一些额外的代码,但为了使所有内容都完好无损,我正在粘贴所有内容。
ScrollMenu.h 类
class ScrollMenu:public CCMenu
{
public:
ScrollMenu();
virtual ~ScrollMenu(){};
bool isMovedGesture_;
bool istabBar_;
CCMenuItem * previousSelectedItem_;
static ScrollMenu* create(CCMenuItem* item,...);
virtual void registerWithTouchDispatcher();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch *touch, CCEvent* event);
CREATE_FUNC(ScrollMenu);
};
class ScrollMenuLoader : public cocos2d::extension::CCNodeLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ScrollMenuLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(ScrollMenu);
};
ScrollMenu.cpp 类
#include "ScrollMenu.h"
ScrollMenu* ScrollMenu::create(CCMenuItem* item, ...)
{
va_list args;
va_start(args, item);
ScrollMenu *pRet = new ScrollMenu();
if (pRet && pRet->initWithItems(item,args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
ScrollMenu::ScrollMenu()
{
isMovedGesture_ = false;
}
void ScrollMenu::registerWithTouchDispatcher()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}
bool ScrollMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
return CCMenu::ccTouchBegan(touch, event);
}
void ScrollMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
isMovedGesture_ = true;
}
void ScrollMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
CCMenuItem * currentItem = this->itemForTouch(touch);
if(!currentItem && isMovedGesture_ && m_pSelectedItem)
{
if(!istabBar_ || (previousSelectedItem_ && previousSelectedItem_ != m_pSelectedItem))
{
m_pSelectedItem->unselected();
}
}
else if(currentItem)
{
if(currentItem == m_pSelectedItem)
{
if(!isMovedGesture_)
{
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
else{
if(previousSelectedItem_ != m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
}
}
else
{
if(isMovedGesture_)
{
m_pSelectedItem->unselected();
m_pSelectedItem = currentItem;
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
}
if (!istabBar_) {
currentItem->unselected();
}
}
m_eState = kCCMenuStateWaiting;
isMovedGesture_ = false;
}