cocos2d-x的两种触摸事件

在Cocos2d-x中,CCTouchDispatcher类负责触摸事件的分发处理,其包含两种触摸事件standardDelegate(标准触摸事件)targetedDelegate(目标触摸事件)

当调用setTouchEnabled(true)设置当前Layer接收触摸事件的时候,默认是开启标准触摸事件。

void CCLayer::setTouchEnabled(bool enabled)
{
    if (m_bTouchEnabled != enabled)
    {
        m_bTouchEnabled = enabled;
        if (m_bRunning)
        {
            if (enabled)
            {   //注册触摸分发事件
                this->registerWithTouchDispatcher();
            }
            else
            {
                //移除分发事件
                CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
            }
        }
    }
}
void CCLayer::registerWithTouchDispatcher()
{
    CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();

    //使用lua脚本时的触摸事件注册
    if (m_pScriptTouchHandlerEntry)
    {
	    if (m_pScriptTouchHandlerEntry->isMultiTouches())
	    {
	       pDispatcher->addStandardDelegate(this,0);
	       LUALOG("[LUA] Add multi-touches event handler: %d",m_pScriptTouchHandlerEntry->getHandler());
	    }
	    else
	    {
	       pDispatcher->addTargetedDelegate(this,m_pScriptTouchHandlerEntry->getPriority(),m_pScriptTouchHandlerEntry->getSwallowsTouches());
	       LUALOG("[LUA] Add touch event handler: %d",m_pScriptTouchHandlerEntry->getHandler());
	    }
    }
    else
    {
        //默认的触摸类型是kCCTouchesAllAtOnce,也就是多点触摸(标准触摸)
        if( m_eTouchMode == kCCTouchesAllAtOnce ) {
            pDispatcher->addStandardDelegate(this,0);
        } else {//如果触摸类型是kCCTouchesOneByOne,则开启的是单点触摸(目标触摸)
            pDispatcher->addTargetedDelegate(this,m_nTouchPriority,true);
        }
    }
}
//添加标准的触摸事件
void addStandardDelegate(CCTouchDelegate *pDelegate,int nPriority);
//添加目标的触摸事件
void addTargetedDelegate(CCTouchDelegate *pDelegate,int nPriority,bool bSwallowsTouches);
//标准触摸事件的回调函数,因为是多点触控,所以它所接收的参数是一个坐标的集合
virtual void ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesCancelled(CCSet *pTouches,CCEvent *pEvent);

//目标触摸事件的回调函数,接收的是单个点的位置坐标
virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);
<span style="font-size:14px;color:#ff0000;"><strong>开启标准触摸事件的方式</strong></span>
bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    //第一种 开启标准触摸事件的方式
    this->setTouchEnabled(true);
    
    //第二种 开启标准触摸事件的方式
    //this->registerWithTouchDispatcher();
}

void HelloWorld::onExit()
{
    //第一种 关闭标准触摸事件的方式
    this->setTouchEnabled(false);
    
    //第二种 关闭标准触摸事件的方式
    //CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
<pre name="code" class="html">//获取标准触摸事件的触摸点
void TestController::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent)
{
    CCSetIterator it = pTouches->begin();
    CCTouch* touch = (CCTouch*)(*it);

    CCPoint beginPos = touch->getLocation();    
}


 //标准触摸事件的回调void HelloWorld::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent){}void HelloWorld::ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent){}void HelloWorld::ccTouchesCancelled(CCSet *pTouches,CCEvent *pEvent){}void HelloWorld::ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent){} 
 

开启目标触摸事件的方式

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    //第一种 开启目标触摸事件的方式
    this->setTouchEnabled(true);
    this->setTouchMode(kCCTouchesOneByOne);
    
    //第二种 开启目标触摸事件的方式
    //CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-1,true);
}

void HelloWorld::onExit()
{
    //第一种 关闭目标触摸事件的方式
    this->setTouchEnabled(false);
    
    //第二种 关闭目标触摸事件的方式
    //CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
//目标触摸事件回调
bool HelloWorld::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){return true;}
void HelloWorld::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){}
void HelloWorld::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){}
void HelloWorld::ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent){}
</pre><pre name="code" class="cpp" style="margin-top: 0px; margin-bottom: 10px; font-size: 13px; line-height: 24px; background-color: rgb(255,255);">

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...