博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx 点击事件分析(3)
阅读量:4217 次
发布时间:2019-05-26

本文共 5960 字,大约阅读时间需要 19 分钟。

1、在cocos2dx 点击事件分析(2)中,我们已经从java端分析了,单手触摸和多手触摸屏幕。num    --- 1,不论单点触摸还是多点触摸,这个值都是1ids[]  --- 触摸事件的IDvoid CCEGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]){    CCSet set;    for (int i = 0; i < num; ++i)    {        int id = ids[i];        float x = xs[i];        float y = ys[i];	//static CCDictionary s_TouchesIntergerDict 存放手指触摸ID,每多一只手,就会产生一个新的ID        CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);        int nUnusedIndex = 0;        // it is a new touch        if (pIndex == NULL)        {            nUnusedIndex = getUnUsedIndex();            // The touches is more than MAX_TOUCHES ?            if (nUnusedIndex == -1) {                CCLOG("The touches is more than MAX_TOUCHES, nUnusedIndex = %d", nUnusedIndex);                continue;            }	    //s_pTouches 存放触摸信息,CCTouch类中,有一个m_nId的成员变量,和这里就对应nUnusedIndex	    //就对应起来了            CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();			pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x) / m_fScaleX,                                      (y - m_obViewPortRect.origin.y) / m_fScaleY);                        //CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);                        CCInteger* pInterObj = new CCInteger(nUnusedIndex);            s_TouchesIntergerDict.setObject(pInterObj, id);            set.adsdObject(pTouch);            pInterObj->release();        }    }    if (set.count() == 0)    {        CCLOG("touchesBegan: count = 0");        return;    }    //根据我的分析,在这里,set的set.count()值只能是1。    m_pDelegate->touchesBegan(&set, NULL);}-->>void CCTouchDispatcher::touchesBegan(CCSet *touches, CCEvent *pEvent){    if (m_bDispatchEvents)    {        this->touches(touches, pEvent, CCTOUCHBEGAN);    }}-->>//// dispatch events//void CCTouchDispatcher::touches(CCSet *pTouches, CCEvent *pEvent, unsigned int uIndex){   //这个里面的内容,后面分析}验证:cocos2dx的TestCpp例子中有个MutiTouchTest.cpp的例子,大家可以在那个例子里面打印一些信息。下面是我添加了一些打印信息后的源码:#include "MutiTouchTest.h"#include "cocos2d.h"static ccColor3B s_TouchColors[CC_MAX_TOUCHES] = {    ccYELLOW,    ccBLUE,    ccGREEN,    ccRED,    ccMAGENTA};class TouchPoint : public CCNode{public:    TouchPoint()    {        setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));    }    virtual void draw()    {        ccDrawColor4B(m_TouchColor.r, m_TouchColor.g, m_TouchColor.b, 255);        glLineWidth(10);        ccDrawLine( ccp(0, m_pTouchPoint.y), ccp(getContentSize().width, m_pTouchPoint.y) );        ccDrawLine( ccp(m_pTouchPoint.x, 0), ccp(m_pTouchPoint.x, getContentSize().height) );        glLineWidth(1);        ccPointSize(30);        ccDrawPoint(m_pTouchPoint);    }    void setTouchPos(const CCPoint& pt)    {        m_pTouchPoint = pt;    }    void setTouchColor(ccColor3B color)    {        m_TouchColor = color;    }    static TouchPoint* touchPointWithParent(CCNode* pParent)    {        TouchPoint* pRet = new TouchPoint();        pRet->setContentSize(pParent->getContentSize());        pRet->setAnchorPoint(ccp(0.0f, 0.0f));        pRet->autorelease();        return pRet;    }private:    CCPoint m_pTouchPoint;    ccColor3B m_TouchColor;};bool MutiTouchTestLayer::init(){    if (CCLayer::init())    {        setTouchEnabled(true);        return true;    }    return false;}static CCDictionary s_dic;void MutiTouchTestLayer::registerWithTouchDispatcher(void){    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);}void MutiTouchTestLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){    CCLog("ccTouchesBegan++++++ %d", pTouches->count());    CCSetIterator iter = pTouches->begin();    for (; iter != pTouches->end(); iter++)    {        CCTouch* pTouch = (CCTouch*)(*iter);        TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this);        CCPoint location = pTouch->getLocation();        pTouchPoint->setTouchPos(location);        pTouchPoint->setTouchColor(s_TouchColors[pTouch->getID()]);        addChild(pTouchPoint);        s_dic.setObject(pTouchPoint, pTouch->getID());        CCLog("ccTouchesBegan++ID++++ %d", pTouch->getID());    }    }void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent){    CCLog("ccTouchesMoved++++++ %d", pTouches->count());    CCSetIterator iter = pTouches->begin();    for (; iter != pTouches->end(); iter++)    {        CCTouch* pTouch = (CCTouch*)(*iter);        TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());        CCPoint location = pTouch->getLocation();        pTP->setTouchPos(location);        CCLog("ccTouchesMoved++ID++++ %d", pTouch->getID());    }}void MutiTouchTestLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){    CCSetIterator iter = pTouches->begin();    for (; iter != pTouches->end(); iter++)    {        CCTouch* pTouch = (CCTouch*)(*iter);        TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());        removeChild(pTP, true);        s_dic.removeObjectForKey(pTouch->getID());    }}void MutiTouchTestLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent){    ccTouchesEnded(pTouches, pEvent);}void MutiTouchTestScene::runThisTest(){    MutiTouchTestLayer* pLayer = MutiTouchTestLayer::create();    addChild(pLayer, 0);    CCDirector::sharedDirector()->replaceScene(this);}1、03-30 13:13:14.192: D/cocos2d-x debug info(22995): ccTouchesBegan++++++ 103-30 13:13:14.192: D/cocos2d-x debug info(22995): ccTouchesBegan++ID++++ 003-30 13:13:14.312: D/cocos2d-x debug info(22995): ccTouchesBegan++++++ 103-30 13:13:14.312: D/cocos2d-x debug info(22995): ccTouchesBegan++ID++++ 12、03-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++++++ 203-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++ID++++ 103-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++ID++++ 0
总结:在ccTouchesBegan函数中,即使是多点触控,每次也只会有一个触摸事件,但是前后两个手指的触摸事件会分配不同的ID,而在ccTouchesMoved里面,会同时传递多点触控的所有点的触摸信息。

转载地址:http://wxsmi.baihongyu.com/

你可能感兴趣的文章
基于微区块链的V2X地理动态入侵检测
查看>>
面向V2C场景的ADAS数字孪生模型构建方法
查看>>
Comma2k19数据集使用
查看>>
面向自动驾驶车辆验证的抽象仿真场景生成
查看>>
一种应用于GPS反欺骗的基于MLE的RAIM改进方法
查看>>
揭秘汽车演化与变革,上海控安在华东师大举办普陀区科普学术论坛
查看>>
筑牢网络安全基座,安全护航经济数字化转型大会成功举办
查看>>
单元测试工具:单元测试的测试前置驱动条件
查看>>
汽车智不智能?“智能座舱”有话说
查看>>
自动驾驶汽车CAN总线数字孪生建模(一)
查看>>
自动驾驶汽车CAN总线数字孪生建模(二)
查看>>
自动驾驶汽车GPS系统数字孪生建模(一)
查看>>
自动驾驶汽车GPS系统数字孪生建模(二)
查看>>
上海控安入选首批工控安全防护能力贯标咨询机构名单
查看>>
自动驾驶汽车传感器数字孪生建模(一)
查看>>
自动驾驶汽车传感器数字孪生建模(二)
查看>>
车载数字孪生预期功能安全未知危害分析技术
查看>>
自动驾驶汽车以太网数字孪生建模(一)
查看>>
自动驾驶汽车以太网数字孪生建模(二)
查看>>
初识软件定义汽车
查看>>