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

cocos2d-x中关于touch事件的响应

发布时间:2020-12-14 16:28:26 所属栏目:百科 来源:网络整理
导读:?? 原作者:有缘人 来源:新浪微博 地址: http://blog.sina.com.cn/s/blog_6ac2c7260102vvdu.html 一、 touch 事件响应分为单点触摸响应和多点触摸响应。 单点触摸响应需要重载的方法: virtual boolccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); virtua
??

原作者:有缘人 来源:新浪微博 地址:http://blog.sina.com.cn/s/blog_6ac2c7260102vvdu.html


一、touch事件响应分为单点触摸响应和多点触摸响应。

单点触摸响应需要重载的方法:

virtual boolccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);

virtual voidccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);

virtual voidccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);

virtual voidccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);

多点触摸需要重载的方法:

virtual voidccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);

virtual void ccTouchesMoved(CCSet*pTouches,CCEvent *pEvent);

virtual voidccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);

virtual voidccTouchesCancelled(CCSet *pTouches,CCEvent *pEvent);

二、单点触摸本质上需要调用的方法,即设置touch的代理

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-129,true);

多点触摸本质上需要调用的方法

CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,-129);

所以,只要调用了以上方法,就可以实现触摸响应,就不再需要调用setTouchEnabled(true);

三、setTouchEnabled的方法实现

void CCLayer::setTouchEnabled(bool enabled)

{

    if (m_bTouchEnabled != enabled)

    {

        m_bTouchEnabled = enabled;

        if (m_bRunning)

        {

            if (enabled)

            {

                this->registerWithTouchDispatcher();

            }

            else

            {

                // have problems?

                CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

            }

        }

    }

}

这段代码最主要的一句this->registerWithTouchDispatcher();

这个方法中就是调用第二点中提到的两个delegate添加方法。

所以,咱们实现touch响应的话,需要重载 registerWithTouchDispatcher()方法,并在这个方法中添加实现

void HelloWorld::registerWithTouchDispatcher()

{

   CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,-129);

   //CDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,true);

}

由此,实现touch事件响应需要做的操作:

1.重载registerWithTouchDispatcher()并实现

2.setTouchEnabled(true)

3.重载touch或者touches系列方法

四、在CCLayerregisterWithTouchDispatcher()中有关于m_eTouchMode的判断,并且有setTouchMode()方法,参数有两种:kCCTouchesOneByOne kCCTouchesAllAtOnce,分别代表单点触摸和多点触摸

touchModekCCTouchesOneByOne 时,调用的是addTargetedDelegate方法;当touchModekCCTouchesAllAtOnce时,调用的是addStandardDelegate方法

所以,“重载registerWithTouchDispatcher()并实现”可以由setTouchMode()方法来替换

(编辑:李大同)

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

    推荐文章
      热点阅读