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

前端之家收集整理的这篇文章主要介绍了cocos2dx touch 单击、双击、三连击和长按事件处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
分类cocos2d-x 1435人阅读 评论(0) 收藏 举报
  1. #include"TouchTest.h"
  2. TouchTest::TouchTest()
  3. {
  4. m_longProgress=false;
  5. }
  6. boolisTouch=false;
  7. boolisMoved=intpressTimes=0;
  8. inttouchCounts=0;
  9. voidTouchTest::updateSingleDelay(floatft)
  10. {
  11. if(touchCounts==1){
  12. onSingleCLick();
  13. touchCounts=0;
  14. }
  15. voidTouchTest::updateDoubleDelay(floatft)
  16. if(touchCounts==2)
  17. onDoubleClick();
  18. voidTouchTest::updatelongprogress(if(isTouch){
  19. pressTimes++;
  20. if(pressTimes>=2){
  21. true;
  22. onLongPressed();
  23. else
  24. pressTimes=0;
  25. longlongTouchTest::getCurrentTime()
  26. structtimevaltm;
  27. gettimeofday(&tm,NULL);
  28. return(long)(tm.tv_sec*1000+tm.tv_usec/1000);
  29. boolTouchTest::touchBegan(cocos2d::Touch*touch,cocos2d::Event*event)
  30. m_startPoint=touch->getLocation();
  31. isTouch=m_startTime=getCurrentTime();
  32. //处理长按事件
  33. this->schedule(schedule_selector(TouchTest::updatelongprogress),1);
  34. returntrue;
  35. voidTouchTest::touchMoved(cocos2d::Touch*touch,cocos2d::Event*event)
  36. isMoved=PointcurPoint=touch->getLocation();
  37. onMove(curPoint);
  38. voidTouchTest::touchEnded(cocos2d::Touch*touch,255); font-weight:bold; background-color:inherit">this->unschedule(schedule_selector(TouchTest::updatelongprogress));
  39. //如果刚完成长按事件则把按下次数清零长按状态置空直接返回不继续执行
  40. if(m_longProgress){
  41. return;
  42. m_endPoint=touch->getLocation();
  43. longendTime=getCurrentTime();
  44. longtimeDis=endTime-m_startTime;
  45. E_SWIP_DIRdir=GetSwipDir(m_startPoint,m_endPoint,timeDis);
  46. if(dir!=E_INVAILD){
  47. onSwip(m_startPoint,dir);
  48. return;
  49. //做连击判断
  50. if(isMoved){
  51. if(touchCounts==2){
  52. onThreeClick();
  53. elsethis->scheduleOnce(schedule_selector(TouchTest::updateDoubleDelay),0.25);
  54. touchCounts++;
  55. if(touchCounts==0){
  56. this->scheduleOnce(schedule_selector(TouchTest::updateSingleDelay),255); background-color:inherit">}
    #include<iostream>
  1. #include"cocos2d.h"
  2. USING_NS_CC;
  3. constintminSwipdistance=100;
  4. intminSwiptime=1000;//毫秒
  5. intmaxClickedDis=20;
  6. enumE_SWIP_DIR
  7. E_INVAILD,
  8. E_LEFT,
  9. E_RIGHT,255); background-color:inherit">E_UP,255); background-color:inherit">E_DOWN
  10. };
  11. classTouchTest:publicLayer
  12. public:
  13. TouchTest();
  14. booltouchBegan(Touch*touch,Event*event);
  15. voidtouchMoved(Touch*touch,Event*event);
  16. voidtouchEnded(Touch*touch,255); font-weight:bold; background-color:inherit">voidtouchCancel(Touch*touch,255); font-weight:bold; background-color:inherit">voidonTouchesBegan(conststd::vector<Touch*>&touches,Event*unused_event);
  17. voidonTouchesMoved(voidonTouchesEnded(voidonTouchesCancelled(voidupdateSingleDelay(float);
  18. voidupdateDoubleDelay(float);
  19. voidupdatelongprogress(longgetCurrentTime();
  20. voidstopSchedule();
  21. private:
  22. E_SWIP_DIRGetSwipDir(Pointstart,Pointend,longtimeDis);
  23. public:
  24. virtualvoidonSingleCLick();//单击
  25. voidonDoubleClick();//双击
  26. voidonThreeClick();//3连击
  27. voidonLongPressed();//长按
  28. voidonMove(Point);//移动
  29. voidonSwip(Point,Point,E_SWIP_DIR);//滑动

猜你在找的Cocos2d-x相关文章