加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x的两种触摸事件

发布时间:2020-12-14 19:55:45 所属栏目:百科 来源:网络整理
导读:在Cocos2d-x中,CCTouchDispatcher类负责触摸事件的分发处理,其包含两种触摸事件 standardDelegate(标准触摸事件) 、 targetedDelegate(目标触摸事件) 当调用setTouchEnabled ( true )设置当前Layer接收触摸事件的时候,默认是开启标准触摸事件。 void CCLa

在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);">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读