码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx 点击事件分析(3)

时间:2015-03-30 14:43:01      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

1、在cocos2dx 点击事件分析(2)中,我们已经从java端分析了,单手触摸和多手触摸屏幕。
num    --- 1,不论单点触摸还是多点触摸,这个值都是1
ids[]  --- 触摸事件的ID
void 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++++++ 1
03-30 13:13:14.192: D/cocos2d-x debug info(22995): ccTouchesBegan++ID++++ 0
03-30 13:13:14.312: D/cocos2d-x debug info(22995): ccTouchesBegan++++++ 1
03-30 13:13:14.312: D/cocos2d-x debug info(22995): ccTouchesBegan++ID++++ 1

2、
03-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++++++ 2
03-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++ID++++ 1
03-30 13:13:14.773: D/cocos2d-x debug info(22995): ccTouchesMoved++ID++++ 0

总结:
在ccTouchesBegan函数中,即使是多点触控,每次也只会有一个触摸事件,
但是前后两个手指的触摸事件会分配不同的ID,而在ccTouchesMoved里面,
会同时传递多点触控的所有点的触摸信息。

cocos2dx 点击事件分析(3)

标签:

原文地址:http://blog.csdn.net/tianxiawuzhei/article/details/44749517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!