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

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

 

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

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

  virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);微信

  virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);网站

  virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);this

  virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);spa

    多点触摸须要重载的方法:.net

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

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

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

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

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

因此,只要调用了以上方法,就能够实现触摸响应,就再也不须要调用setTouchEnabled(true);

3、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,-129,true);
}

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

1.重载registerWithTouchDispatcher()并实现

2.setTouchEnabled(true)

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

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

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

因此,“重载registerWithTouchDispatcher()并实现”能够由setTouchMode()方法来替换

 

 

欢迎关注关东升新浪微博@tony_ 关东升。

关注智捷课堂微信公共平台,了解最新技术文章、图书、教程信息

更多精品iOSCocos、移动设计课程请关注智捷课堂官方网站:http://www.zhijieketang.com

智捷课堂论坛网站:http://51work6.com/forum.php

相关文章
相关标签/搜索