/* 功能 * 1. 弹出层的基类, 比下层拥有更高的优先级, 用于屏蔽下层,及本层外触摸事件 * 2. 提供一个容量,及相应方法,用于装纳需要处理事件的对象 CCTouchDelegate*对象, */ #ifndef __MythLeague__UpperLayer__ #define __MythLeague__UpperLayer__ #include "Global/Constants.h" #include "CommonLayer/TouchLogicLayer.h" class UpperLayer : public TouchLogicLayer{ public: UpperLayer(); virtual ~UpperLayer(); bool init(); void onEnter(); void onExit(); void registerWithTouchDispatcher(); void appendToTouchDispatch(cocos2d::CCTouchDelegate* p_touchableObject); #pragma mark 触摸相关 //开始 bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); //移动 void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); //结束 void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); //点击home键或其它方式引起的取消 void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); public: //cocos2d::CCArray* m_touchDispatchTable; std::vector<cocos2d::CCTouchDelegate*> m_touchDispatchTable; int m_iIndex;//m_iIndex-1为实际索引 }; #endif /* defined(__MythLeague__UpperLayer__) */ // // #include "UpperLayer.h" using namespace cocos2d; UpperLayer::UpperLayer(){ } UpperLayer::~UpperLayer(){ } bool UpperLayer::init(){ bool l_bResult = true; do { if(!TouchLogicLayer::init()){ l_bResult = false; } } while (0); return l_bResult; } void UpperLayer::onEnter(){ TouchLogicLayer::onEnter(); } void UpperLayer::onExit(){ TouchLogicLayer::onExit(); } void UpperLayer::registerWithTouchDispatcher() { cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority-1, true); } void UpperLayer::appendToTouchDispatch(CCTouchDelegate* p_touchableObject){ //断言,p_touchableLayer是CCLayer*类型, (可以是继承) CCAssert(dynamic_cast<CCTouchDelegate*>(p_touchableObject) != NULL, "p_touchableLayer must be a layer"); m_touchDispatchTable.push_back(p_touchableObject); } #pragma mark 触摸相关 //开始 bool UpperLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){ //super TouchLogicLayer::ccTouchBegan(pTouch, pEvent); m_iIndex = Common_Empty; CCTouchDelegate* l_touchAble = NULL; int l_iIndex = 0; for (; l_iIndex<m_touchDispatchTable.size(); l_iIndex++) { l_touchAble = m_touchDispatchTable[l_iIndex]; if (l_touchAble && l_touchAble->ccTouchBegan(pTouch, pEvent)) { m_iIndex = l_iIndex; break; } } return true; } //移动 void UpperLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){ //super TouchLogicLayer::ccTouchMoved(pTouch, pEvent); if (m_iIndex >= 0) { CCTouchDelegate* l_touchAble = m_touchDispatchTable[m_iIndex]; l_touchAble->ccTouchMoved(pTouch, pEvent); } } //结束 void UpperLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){ //super TouchLogicLayer::ccTouchEnded(pTouch, pEvent); if (m_iIndex >= 0) { CCTouchDelegate* l_touchAbleLayer = m_touchDispatchTable[m_iIndex]; l_touchAbleLayer->ccTouchEnded(pTouch, pEvent); } } //点击home键或其它方式引起的取消 void UpperLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){ } 所有的弹出层,只要继承一下这个就可以,他的优点是事件分发由自己来控制,比较灵活