cocos2d-x笔记 ccTouchesBegan、ccTouchesMoved、ccTouchesEnded

#ifndef __MUTITOUCHTEST_H__

#define __MUTITOUCHTEST_H__



#include "../testBasic.h"



class MutiTouchTestLayer : public CCLayer

{

public:

    bool init();



    virtual void registerWithTouchDispatcher(void);

    virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

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

    virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

    virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);



    CREATE_FUNC(MutiTouchTestLayer)

};



class MutiTouchTestScene : public TestScene

{

 public:

  virtual void runThisTest();

};



#endif /* __MUTITOUCHTEST_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)

{

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

//将点对象及其id加到字典中

        s_dic.setObject(pTouchPoint, pTouch->getID());

    }

    



}
void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)

{

    CCSetIterator iter = pTouches->begin();

    for (; iter != pTouches->end(); iter++)

    {

        CCTouch* pTouch = (CCTouch*)(*iter);

//通过id从字典中找出相应的点对象

        TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());

        CCPoint location = pTouch->getLocation();

//然后赋值进去

        pTP->setTouchPos(location);

    }

}
//清除点集

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

}

cocos2d-x笔记 ccTouchesBegan、ccTouchesMoved、ccTouchesEnded

你可能感兴趣的:(cocos2d-x)