cocos2dx 点击事件分析(3)

cocos2dx 点击事件分析(3),第1张

概述1、在cocos2dx 点击事件分析(2)中,我们已经从java端分析了,单手触摸和多手触摸屏幕。num --- 1,不论单点触摸还是多点触摸,这个值都是1ids[] --- 触摸事件的IDvoid CCEGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]){ CCSet
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里面,会同时传递多点触控的所有点的触摸信息。
总结

以上是内存溢出为你收集整理的cocos2dx 点击事件分析(3)全部内容,希望文章能够帮你解决cocos2dx 点击事件分析(3)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1052135.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存