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">}
@H_404_375@#include<iostream>
  • #include"cocos2d.h"
  • USING_NS_CC;
  • constintminSwipdistance=100;
  • intminSwiptime=1000;//毫秒
  • intmaxClickedDis=20;
  • enumE_SWIP_DIR
  • E_INVAILD,
  • E_LEFT,
  • E_RIGHT,255); background-color:inherit">E_UP,255); background-color:inherit">E_DOWN
  • };
  • classTouchTest:publicLayer
  • public:
  • TouchTest();
  • booltouchBegan(Touch*touch,Event*event);
  • voidtouchMoved(Touch*touch,Event*event);
  • 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);
  • voidonTouchesMoved(voidonTouchesEnded(voidonTouchesCancelled(voidupdateSingleDelay(float);
  • voidupdateDoubleDelay(float);
  • voidupdatelongprogress(longgetCurrentTime();
  • voidstopSchedule();
  • private:
  • E_SWIP_DIRGetSwipDir(Pointstart,Pointend,longtimeDis);
  • public:
  • virtualvoidonSingleCLick();//单击
  • voidonDoubleClick();//双击
  • voidonThreeClick();//3连击
  • voidonLongPressed();//长按
  • voidonMove(Point);//移动
  • voidonSwip(Point,Point,E_SWIP_DIR);//滑动
  • 原文链接:https://www.f2er.com/cocos2dx/345411.html

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