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, 255);">


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值