您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx 自定义触摸事件的分发

2015-05-30 13:10 531 查看
cocos2dx 2.x版本是通过CCTouchDispatcher这个类来管理触摸事件的分发

CCTouchDispatcher.h文件修改如下

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2009      Valentin Milea
 http://www.cocos2d-x.org 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#ifndef __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__
#define __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__

#include "CCTouchDelegateProtocol.h"
#include "cocoa/CCObject.h"
#include "cocoa/CCArray.h"

NS_CC_BEGIN

/**
* @addtogroup input
* @{
*/

typedef enum
{
ccTouchSelectorBeganBit = 1 << 0,
ccTouchSelectorMovedBit = 1 << 1,
ccTouchSelectorEndedBit = 1 << 2,
ccTouchSelectorCancelledBit = 1 << 3,
ccTouchSelectorAllBits = ( ccTouchSelectorBeganBit | ccTouchSelectorMovedBit | ccTouchSelectorEndedBit | ccTouchSelectorCancelledBit),
} ccTouchSelectorFlag;

enum {
CCTOUCHBEGAN,
CCTOUCHMOVED,
CCTOUCHENDED,
CCTOUCHCANCELLED,

ccTouchMax,
};

class CCSet;
class CCEvent;

struct ccTouchHandlerHelperData {
// we only use the type
//    void (StandardTouchDelegate::*touchesSel)(CCSet*, CCEvent*);
//    void (TargetedTouchDelegate::*touchSel)(NSTouch*, CCEvent*);
int  m_type;
};

/**
* @js NA
*/
class CC_DLL EGLTouchDelegate
{
public:
/**
* @lua NA
*/
virtual void touchesBegan(CCSet* touches, CCEvent* pEvent) = 0;
/**
* @lua NA
*/
virtual void touchesMoved(CCSet* touches, CCEvent* pEvent) = 0;
/**
* @lua NA
*/
virtual void touchesEnded(CCSet* touches, CCEvent* pEvent) = 0;
/**
* @lua NA
*/
virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent) = 0;
/**
* @lua NA
*/
virtual ~EGLTouchDelegate() {}
};

class CCTouchHandler;
struct _ccCArray;
/** @brief CCTouchDispatcher.
Singleton that handles all the touch events.
The dispatcher dispatches events to the registered TouchHandlers.
There are 2 different type of touch handlers:
- Standard Touch Handlers
- Targeted Touch Handlers

The Standard Touch Handlers work like the CocoaTouch touch handler: a set of touches is passed to the delegate.
On the other hand, the Targeted Touch Handlers only receive 1 touch at the time, and they can "swallow" touches (avoid the propagation of the event).

Firstly, the dispatcher sends the received touches to the targeted touches.
These touches can be swallowed by the Targeted Touch Handlers. If there are still remaining touches, then the remaining touches will be sent
to the Standard Touch Handlers.

@since v0.8.0
@js NA
*/
class CC_DLL CCTouchDispatcher : public CCObject, public EGLTouchDelegate
{
public:
/**
* @lua NA
*/
~CCTouchDispatcher();
/**
* @lua NA
*/
bool init(void);
/**
* @lua NA
*/
CCTouchDispatcher()
: m_pTargetedHandlers(NULL)
, m_pStandardHandlers(NULL)
, m_pHandlersToAdd(NULL)
, m_pHandlersToRemove(NULL)
<span style="color:#ff0000;"> , m_pHandlersToResume(NULL)</span>
{}

public:
/** Whether or not the events are going to be dispatched. Default: true */
bool isDispatchEvents(void);
void setDispatchEvents(bool bDispatchEvents);

/** Adds a standard touch delegate to the dispatcher's list.
* See StandardTouchDelegate description.
* IMPORTANT: The delegate will be retained.
* @lua NA
*/
void addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority);

/** Adds a targeted touch delegate to the dispatcher's list.
* See TargetedTouchDelegate description.
* IMPORTANT: The delegate will be retained.
* @lua NA
*/
void addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches);

/** Removes a touch delegate.
* The delegate will be released
* @lua NA
*/
void removeDelegate(CCTouchDelegate *pDelegate);

/** Removes all touch delegates, releasing all the delegates
* @lua NA
*/
void removeAllDelegates(void);

/** Changes the priority of a previously added delegate. The lower the number,
* the higher the priority
* @lua NA
*/
void setPriority(int nPriority, CCTouchDelegate *pDelegate);
/**
* @lua NA
*/
void touches(CCSet *pTouches, CCEvent *pEvent, unsigned int uIndex);
/**
* @lua NA
*/
virtual void touchesBegan(CCSet* touches, CCEvent* pEvent);
/**
* @lua NA
*/
virtual void touchesMoved(CCSet* touches, CCEvent* pEvent);
/**
* @lua NA
*/
virtual void touchesEnded(CCSet* touches, CCEvent* pEvent);
/**
* @lua NA
*/
virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent);

<span style="color:#ff0000;">void stopDispatcherForceDelegate();
void resumeDispatcherForceDelegate();</span>
public:
/**
* @lua NA
*/
CCTouchHandler* findHandler(CCTouchDelegate *pDelegate);
protected:
void forceRemoveDelegate(CCTouchDelegate *pDelegate);
void forceAddHandler(CCTouchHandler *pHandler, CCArray* pArray);
void forceRemoveAllDelegates(void);
void rearrangeHandlers(CCArray* pArray);
CCTouchHandler* findHandler(CCArray* pArray, CCTouchDelegate *pDelegate);

protected:
CCArray* m_pTargetedHandlers;
CCArray* m_pStandardHandlers;

bool m_bLocked;
bool m_bToAdd;
bool m_bToRemove;
CCArray* m_pHandlersToAdd;
<span style="color:#ff0000;">CCArray* m_pHandlersToResume;</span>
struct _ccCArray *m_pHandlersToRemove;
bool m_bToQuit;
bool m_bDispatchEvents;

// 4, 1 for each type of event
struct ccTouchHandlerHelperData m_sHandlerHelperData[ccTouchMax];
};

// end of input group
/// @}

NS_CC_END

#endif // __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__


CCTouchDispatcher.cpp修改如下

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2009      Valentin Milea
 http://www.cocos2d-x.org 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#include "CCTouchDispatcher.h"
#include "CCTouchHandler.h"
#include "cocoa/CCArray.h"
#include "cocoa/CCSet.h"
#include "CCTouch.h"
#include "textures/CCTexture2D.h"
#include "support/data_support/ccCArray.h"
#include "ccMacros.h"
#include <algorithm>

NS_CC_BEGIN

/**
* Used for sort
*/
static int less(const CCObject* p1, const CCObject* p2)
{
return ((CCTouchHandler*)p1)->getPriority() < ((CCTouchHandler*)p2)->getPriority();
}

bool CCTouchDispatcher::isDispatchEvents(void)
{
return m_bDispatchEvents;
}

void CCTouchDispatcher::setDispatchEvents(bool bDispatchEvents)
{
m_bDispatchEvents = bDispatchEvents;
}

/*
+(id) allocWithZone:(CCZone *)zone
{
@synchronized(self) {
CCAssert(sharedDispatcher == nil, @"Attempted to allocate a second instance of a singleton.");
return [super allocWithZone:zone];
}
return nil; // on subsequent allocation attempts return nil
}
*/

bool CCTouchDispatcher::init(void)
{
m_bDispatchEvents = true;
m_pTargetedHandlers = CCArray::createWithCapacity(8);
m_pTargetedHandlers->retain();
m_pStandardHandlers = CCArray::createWithCapacity(4);
m_pStandardHandlers->retain();
m_pHandlersToAdd = CCArray::createWithCapacity(8);
m_pHandlersToAdd->retain();

m_pHandlersToResume = CCArray::createWithCapacity(8);
m_pHandlersToResume->retain();

m_pHandlersToRemove = ccCArrayNew(8);

m_bToRemove = false;
m_bToAdd = false;
m_bToQuit = false;
m_bLocked = false;

m_sHandlerHelperData[CCTOUCHBEGAN].m_type = CCTOUCHBEGAN;
m_sHandlerHelperData[CCTOUCHMOVED].m_type = CCTOUCHMOVED;
m_sHandlerHelperData[CCTOUCHENDED].m_type = CCTOUCHENDED;
m_sHandlerHelperData[CCTOUCHCANCELLED].m_type = CCTOUCHCANCELLED;

return true;
}

CCTouchDispatcher::~CCTouchDispatcher(void)
{
CC_SAFE_RELEASE(m_pTargetedHandlers);
CC_SAFE_RELEASE(m_pStandardHandlers);
CC_SAFE_RELEASE(m_pHandlersToAdd);
CC_SAFE_RELEASE(m_pHandlersToResume);

ccCArrayFree(m_pHandlersToRemove);
m_pHandlersToRemove = NULL;
}

//
// handlers management
//
void CCTouchDispatcher::forceAddHandler(CCTouchHandler *pHandler, CCArray *pArray)
{
unsigned int u = 0;

CCObject* pObj = NULL;
CCARRAY_FOREACH(pArray, pObj)
{
CCTouchHandler *h = (CCTouchHandler *)pObj;
if (h)
{
if (h->getPriority() < pHandler->getPriority())
{
++u;
}

if (h->getDelegate() == pHandler->getDelegate())
{
CCAssert(0, "");
return;
}
}
}

pArray->insertObject(pHandler, u);
}

void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{
CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
if (! m_bLocked)
{
forceAddHandler(pHandler, m_pStandardHandlers);
}
else
{
/* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
* Refer issue #752(cocos2d-x)
*/
if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
{
ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
return;
}

m_pHandlersToAdd->addObject(pHandler);
m_bToAdd = true;
}
}

void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
{
CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
if (! m_bLocked)
{
forceAddHandler(pHandler, m_pTargetedHandlers);
}
else
{
/* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
* Refer issue #752(cocos2d-x)
*/
if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
{
ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
return;
}

m_pHandlersToAdd->addObject(pHandler);
m_bToAdd = true;
}
}

void CCTouchDispatcher::forceRemoveDelegate(CCTouchDelegate *pDelegate)
{
CCTouchHandler *pHandler;

// XXX: remove it from both handlers ???

// remove handler from m_pStandardHandlers
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pStandardHandlers, pObj)
{
pHandler = (CCTouchHandler*)pObj;
if (pHandler && pHandler->getDelegate() == pDelegate)
{
m_pStandardHandlers->removeObject(pHandler);
break;
}
}

// remove handler from m_pTargetedHandlers
CCARRAY_FOREACH(m_pTargetedHandlers, pObj)
{
pHandler = (CCTouchHandler*)pObj;
if (pHandler && pHandler->getDelegate() == pDelegate)
{
m_pTargetedHandlers->removeObject(pHandler);
break;
}
}
}

void CCTouchDispatcher::removeDelegate(CCTouchDelegate *pDelegate)
{
if (pDelegate == NULL)
{
return;
}

if (! m_bLocked)
{
forceRemoveDelegate(pDelegate);
}
else
{
/* If pHandler is contained in m_pHandlersToAdd, if so remove it from m_pHandlersToAdd and return.
* Refer issue #752(cocos2d-x)
*/
CCTouchHandler *pHandler = findHandler(m_pHandlersToAdd, pDelegate);
if (pHandler)
{
m_pHandlersToAdd->removeObject(pHandler);
return;
}

ccCArrayAppendValue(m_pHandlersToRemove, pDelegate);
m_bToRemove = true;
}
}

void CCTouchDispatcher::forceRemoveAllDelegates(void)
{
m_pStandardHandlers->removeAllObjects();
m_pTargetedHandlers->removeAllObjects();
}

void CCTouchDispatcher::removeAllDelegates(void)
{
if (! m_bLocked)
{
forceRemoveAllDelegates();
}
else
{
m_bToQuit = true;
}
}

CCTouchHandler* CCTouchDispatcher::findHandler(CCTouchDelegate *pDelegate)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pTargetedHandlers, pObj)
{
CCTouchHandler* pHandler = (CCTouchHandler*)pObj;
if (pHandler->getDelegate() == pDelegate)
{
return pHandler;
}
}

CCARRAY_FOREACH(m_pStandardHandlers, pObj)
{
CCTouchHandler* pHandler = (CCTouchHandler*)pObj;
if (pHandler->getDelegate() == pDelegate)
{
return pHandler;
}
}

return NULL;
}

CCTouchHandler* CCTouchDispatcher::findHandler(CCArray* pArray, CCTouchDelegate *pDelegate)
{
CCAssert(pArray != NULL && pDelegate != NULL, "");

CCObject* pObj = NULL;
CCARRAY_FOREACH(pArray, pObj)
{
CCTouchHandler* pHandle = (CCTouchHandler*)pObj;
if (pHandle->getDelegate() == pDelegate)
{
return pHandle;
}
}

return NULL;
}

void CCTouchDispatcher::rearrangeHandlers(CCArray *pArray)
{
std::sort(pArray->data->arr, pArray->data->arr + pArray->data->num, less);
}

void CCTouchDispatcher::setPriority(int nPriority, CCTouchDelegate *pDelegate)
{
CCAssert(pDelegate != NULL, "");

CCTouchHandler *handler = NULL;

handler = this->findHandler(pDelegate);

CCAssert(handler != NULL, "");

if (handler->getPriority() != nPriority)
{
handler->setPriority(nPriority);
this->rearrangeHandlers(m_pTargetedHandlers);
this->rearrangeHandlers(m_pStandardHandlers);
}
}

//
// dispatch events
//
void CCTouchDispatcher::touches(CCSet *pTouches, CCEvent *pEvent, unsigned int uIndex)
{
CCAssert(uIndex >= 0 && uIndex < 4, "");

CCSet *pMutableTouches;
m_bLocked = true;

// optimization to prevent a mutable copy when it is not necessary
unsigned int uTargetedHandlersCount = m_pTargetedHandlers->count();
unsigned int uStandardHandlersCount = m_pStandardHandlers->count();
bool bNeedsMutableSet = (uTargetedHandlersCount && uStandardHandlersCount);

pMutableTouches = (bNeedsMutableSet ? pTouches->mutableCopy() : pTouches);

struct ccTouchHandlerHelperData sHelper = m_sHandlerHelperData[uIndex];
//
// process the target handlers 1st
//
if (uTargetedHandlersCount > 0)
{
CCTouch *pTouch;
CCSetIterator setIter;
for (setIter = pTouches->begin(); setIter != pTouches->end(); ++setIter)
{
pTouch = (CCTouch *)(*setIter);

CCTargetedTouchHandler *pHandler = NULL;
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pTargetedHandlers, pObj)
{
pHandler = (CCTargetedTouchHandler *)(pObj);

if (! pHandler)
{
break;
}

bool bClaimed = false;
if (uIndex == CCTOUCHBEGAN)
{
bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent);

if (bClaimed)
{
pHandler->getClaimedTouches()->addObject(pTouch);
}
} else
if (pHandler->getClaimedTouches()->containsObject(pTouch))
{
// moved ended canceled
bClaimed = true;

switch (sHelper.m_type)
{
case CCTOUCHMOVED:
pHandler->getDelegate()->ccTouchMoved(pTouch, pEvent);
break;
case CCTOUCHENDED:
pHandler->getDelegate()->ccTouchEnded(pTouch, pEvent);
pHandler->getClaimedTouches()->removeObject(pTouch);
break;
case CCTOUCHCANCELLED:
pHandler->getDelegate()->ccTouchCancelled(pTouch, pEvent);
pHandler->getClaimedTouches()->removeObject(pTouch);
break;
}
}

if (bClaimed && pHandler->isSwallowsTouches())
{
if (bNeedsMutableSet)
{
pMutableTouches->removeObject(pTouch);
}

break;
}
}
}
}

//
// process standard handlers 2nd
//
if (uStandardHandlersCount > 0 && pMutableTouches->count() > 0)
{
CCStandardTouchHandler *pHandler = NULL;
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pStandardHandlers, pObj)
{
pHandler = (CCStandardTouchHandler*)(pObj);

if (! pHandler)
{
break;
}

switch (sHelper.m_type)
{
case CCTOUCHBEGAN:
pHandler->getDelegate()->ccTouchesBegan(pMutableTouches, pEvent);
break;
case CCTOUCHMOVED:
pHandler->getDelegate()->ccTouchesMoved(pMutableTouches, pEvent);
break;
case CCTOUCHENDED:
pHandler->getDelegate()->ccTouchesEnded(pMutableTouches, pEvent);
break;
case CCTOUCHCANCELLED:
pHandler->getDelegate()->ccTouchesCancelled(pMutableTouches, pEvent);
break;
}
}
}

if (bNeedsMutableSet)
{
pMutableTouches->release();
}

//
// Optimization. To prevent a [handlers copy] which is expensive
// the add/removes/quit is done after the iterations
//
m_bLocked = false;
if (m_bToRemove)
{
m_bToRemove = false;
for (unsigned int i = 0; i < m_pHandlersToRemove->num; ++i)
{
forceRemoveDelegate((CCTouchDelegate*)m_pHandlersToRemove->arr[i]);
}
ccCArrayRemoveAllValues(m_pHandlersToRemove);
}

if (m_bToAdd)
{
m_bToAdd = false;
CCTouchHandler* pHandler = NULL;
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pHandlersToAdd, pObj)
{
pHandler = (CCTouchHandler*)pObj;
if (! pHandler)
{
break;
}

if (dynamic_cast<CCTargetedTouchHandler*>(pHandler) != NULL)
{
forceAddHandler(pHandler, m_pTargetedHandlers);
}
else
{
forceAddHandler(pHandler, m_pStandardHandlers);
}
}

m_pHandlersToAdd->removeAllObjects();
}

if (m_bToQuit)
{
m_bToQuit = false;
forceRemoveAllDelegates();
}
}

void CCTouchDispatcher::touchesBegan(CCSet *touches, CCEvent *pEvent)
{
if (m_bDispatchEvents)
{
this->touches(touches, pEvent, CCTOUCHBEGAN);
}
}

void CCTouchDispatcher::touchesMoved(CCSet *touches, CCEvent *pEvent)
{
if (m_bDispatchEvents)
{
this->touches(touches, pEvent, CCTOUCHMOVED);
}
}

void CCTouchDispatcher::touchesEnded(CCSet *touches, CCEvent *pEvent)
{
if (m_bDispatchEvents)
{
this->touches(touches, pEvent, CCTOUCHENDED);
}
}

void CCTouchDispatcher::touchesCancelled(CCSet *touches, CCEvent *pEvent)
{
if (m_bDispatchEvents)
{
this->touches(touches, pEvent, CCTOUCHCANCELLED);
}
}
void CCTouchDispatcher::stopDispatcherForceDelegate()
{

CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pTargetedHandlers, pObj)
{
m_pHandlersToResume->addObject(pObj);
}
m_pTargetedHandlers->removeAllObjects();
}
void CCTouchDispatcher::resumeDispatcherForceDelegate()
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pHandlersToResume, pObj)
{
if(!m_bLocked)
{
m_pTargetedHandlers->addObject(pObj);
}
else
{
CCTouchHandler *h = (CCTouchHandler *)pObj;

if (ccCArrayContainsValue(m_pHandlersToRemove, h))
{
ccCArrayRemoveValue(m_pHandlersToRemove, h);
return;
}

m_pHandlersToAdd->addObject(pObj);
m_bToAdd = true;
}
}

m_pHandlersToResume->removeAllObjects();
}
NS_CC_END


添加好接口后 我们就可以调用了

PopLayer.h

#ifndef __POP_LAYER_H__
#define __POP_LAYER_H__

#include "cocos2d.h"

class PopLayer : public cocos2d::CCLayerColor
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(PopLayer);

};

#endif // __POP_LAYER_H__


PopLayer.cpp

#include "PopLayer.h"

USING_NS_CC;

// on "init" you need to initialize your instance
bool PopLayer::init()
{

CCDirector::sharedDirector()->getTouchDispatcher()->stopDispatcherForceDelegate();
//////////////////////////////
// 1. super init first
if ( !CCLayerColor::initWithColor(ccc4(125,125,125,125)) )
{
return false;
}

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(PopLayer::menuCloseCallback));

pCloseItem->setPosition(ccp(origin.x + visibleSize.width/2 - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));

// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);

/////////////////////////////
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

CCLabelTTF* pLabel = CCLabelTTF::create(" Pop  Layer", "Arial", 24);

// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));

// add the label as a child to this layer
this->addChild(pLabel, 1);
setTouchEnabled(true);
return true;
}

void PopLayer::menuCloseCallback(CCObject* pSender)
{
CCLog("PopLayer button clicked !!! ");
this->removeFromParent();
CCDirector::sharedDirector()->getTouchDispatcher()->resumeDispatcherForceDelegate();

}
在调用这个层的时候 将之前要相应的事件都移除,在这个层消失时 ,再将移除的事件恢复,这样就实现了自己控制触摸事件的分发,弹出的层下面的按钮都不会被响应
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: