cocos2dx touch 单击、双击、三连击和长按事件处理

来源:互联网 发布:幼儿园床垫淘宝 编辑:程序博客网 时间:2024/05/19 04:03
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "TouchTest.h"  
  2.   
  3. TouchTest::TouchTest()  
  4. {  
  5.     m_longProgress=false;  
  6. }  
  7.   
  8.   
  9.   
  10. bool isTouch=false;  
  11.   
  12. bool isMoved=false;  
  13.   
  14. int pressTimes=0;  
  15.   
  16. int touchCounts=0;  
  17.   
  18. void TouchTest::updateSingleDelay(float ft)  
  19. {  
  20.     if (touchCounts == 1) {  
  21.         onSingleCLick();  
  22.         touchCounts=0;  
  23.     }  
  24.       
  25. }  
  26.   
  27. void TouchTest::updateDoubleDelay(float ft)  
  28. {  
  29.     if (touchCounts == 2 )  
  30.     {  
  31.         onDoubleClick();  
  32.         touchCounts=0;  
  33.     }  
  34. }  
  35.   
  36. void TouchTest::updatelongprogress(float ft)  
  37. {  
  38.     if (isTouch) {  
  39.         pressTimes++;  
  40.           
  41.         if (pressTimes >= 2) {  
  42.             m_longProgress=true;  
  43.             onLongPressed();  
  44.               
  45.         }  
  46.     }  
  47.     else  
  48.     {  
  49.         pressTimes=0;  
  50.     }  
  51. }  
  52.   
  53. long long TouchTest::getCurrentTime()  
  54. {  
  55.     struct timeval tm;  
  56.     gettimeofday(&tm, NULL);  
  57.     return (long long)(tm.tv_sec*1000 + tm.tv_usec/1000);  
  58. }  
  59.   
  60.   
  61. bool TouchTest::touchBegan(cocos2d::Touch *touch, cocos2d::Event *event)  
  62. {  
  63.       
  64.     m_startPoint=touch->getLocation();  
  65.   
  66.     isTouch=true;  
  67.           
  68.     m_startTime=getCurrentTime();  
  69.          
  70.         //处理长按事件  
  71.     this->schedule(schedule_selector(TouchTest::updatelongprogress),1);  
  72.       
  73.     return true;  
  74. }  
  75.   
  76. void TouchTest::touchMoved(cocos2d::Touch *touch, cocos2d::Event *event)  
  77. {  
  78.     isMoved=true;  
  79.     Point curPoint=touch->getLocation();  
  80.     onMove(curPoint);  
  81.       
  82. }  
  83.   
  84. void TouchTest::touchEnded(cocos2d::Touch *touch, cocos2d::Event *event)  
  85. {  
  86.     isTouch=false;  
  87.     pressTimes=0;  
  88.     this->unschedule(schedule_selector(TouchTest::updatelongprogress));  
  89.       
  90.     //如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行  
  91.     if (m_longProgress ) {  
  92.         touchCounts=0;  
  93.         m_longProgress=false;  
  94.           
  95.         return;  
  96.     }  
  97.       
  98.     m_endPoint=touch->getLocation();  
  99.       
  100.     long long endTime=getCurrentTime();  
  101.       
  102.     long long timeDis=endTime-m_startTime;  
  103.       
  104.     E_SWIP_DIR dir=GetSwipDir(m_startPoint, m_endPoint,timeDis);  
  105.       
  106.     if ( dir != E_INVAILD) {  
  107.         onSwip(m_startPoint, m_endPoint, dir);  
  108.         return;  
  109.     }  
  110.       
  111.     //做连击判断  
  112.     if (isMoved) {  
  113.         isMoved=false;  
  114.         return;  
  115.     }  
  116.     if (touchCounts == 2) {  
  117.         onThreeClick();  
  118.         touchCounts=0;  
  119.     }  
  120.     else if (touchCounts == 1) {  
  121.         this->scheduleOnce(schedule_selector(TouchTest::updateDoubleDelay), 0.25);  
  122.         touchCounts++;  
  123.     }  
  124.     else if (touchCounts == 0) {  
  125.         this->scheduleOnce(schedule_selector(TouchTest::updateSingleDelay), 0.25);  
  126.         touchCounts++;  
  127.     }  
  128.       
  129.       
  130.       
  131. }  

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "cocos2d.h"  
  3. USING_NS_CC;  
  4.   
  5.   
  6. const int minSwipdistance=100;  
  7. const int minSwiptime=1000;    //毫秒  
  8. const int maxClickedDis=20;  
  9.   
  10. enum E_SWIP_DIR  
  11. {  
  12.     E_INVAILD,  
  13.     E_LEFT,  
  14.     E_RIGHT,  
  15.     E_UP,  
  16.     E_DOWN  
  17. };  
  18.   
  19. class TouchTest:public Layer  
  20. {  
  21. public:  
  22.     TouchTest();  
  23.       
  24.     bool touchBegan(Touch * touch,Event * event);  
  25.     void touchMoved(Touch * touch,Event * event);  
  26.     void touchEnded(Touch * touch,Event * event);  
  27.     void touchCancel(Touch * touch,Event * event);  
  28.       
  29.     void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);  
  30.     void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);  
  31.     void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);  
  32.     void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);  
  33.       
  34.       
  35.     void updateSingleDelay(float);  
  36.     void updateDoubleDelay(float);  
  37.   
  38.     void updatelongprogress(float);  
  39.       
  40.     long long getCurrentTime();  
  41.   
  42.       
  43.     void stopSchedule();  
  44.       
  45. private:  
  46.     E_SWIP_DIR GetSwipDir(Point start,Point end,long long timeDis);  
  47.       
  48. public:  
  49.     virtual void onSingleCLick();        //单击  
  50.     virtual void onDoubleClick();        //双击  
  51.     virtual void onThreeClick();         //3连击  
  52.     virtual void onLongPressed();        //长按  
  53.     virtual void onMove(Point);          //移动  
  54.     virtual void onSwip(Point,Point,E_SWIP_DIR);    //滑动  
0 0