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

  • 相关阅读:
    《数据可视化之美》阅读(二)
    《数据可视化之美》阅读
    D3学习之动画和变换
    Java学习之垃圾回收
    程序员思维修炼 --- 读书笔记(二)
    程序员思维修炼 --- 读书笔记(一)
    Java 学习笔记 ------第六章 继承与多态
    Java 学习笔记 ------第五章 对象封装
    Java 学习笔记 ------第四章 认识对象
    (转)synchronized底层实现原理&CAS操作&偏向锁、轻量级锁,重量级锁、自旋锁、自适应自旋锁、锁消除、锁粗化
  • 原文地址:https://www.cnblogs.com/newlist/p/3238594.html
Copyright © 2020-2023  润新知