cocos2dx touch 单击、双击、三连击和长按事件处理
来源:互联网 发布:邢台学院教务处网络 编辑:程序博客网 时间:2024/05/14 01:27
#include "TouchTest.h"TouchTest::TouchTest(){ m_longProgress=false;}bool isTouch=false;bool isMoved=false;int pressTimes=0;int touchCounts=0;void TouchTest::updateSingleDelay(float ft){ if (touchCounts == 1) { onSingleCLick(); touchCounts=0; } }void TouchTest::updateDoubleDelay(float ft){ if (touchCounts == 2 ) { onDoubleClick(); touchCounts=0; }}void TouchTest::updatelongprogress(float ft){ if (isTouch) { pressTimes++; if (pressTimes >= 2) { m_longProgress=true; onLongPressed(); } } else { pressTimes=0; }}long long TouchTest::getCurrentTime(){ struct timeval tm; gettimeofday(&tm, NULL); return (long long)(tm.tv_sec*1000 + tm.tv_usec/1000);}bool TouchTest::touchBegan(cocos2d::Touch *touch, cocos2d::Event *event){ m_startPoint=touch->getLocation(); isTouch=true; m_startTime=getCurrentTime(); //处理长按事件 this->schedule(schedule_selector(TouchTest::updatelongprogress),1); return true;}void TouchTest::touchMoved(cocos2d::Touch *touch, cocos2d::Event *event){ isMoved=true; Point curPoint=touch->getLocation(); onMove(curPoint); }void TouchTest::touchEnded(cocos2d::Touch *touch, cocos2d::Event *event){ isTouch=false; pressTimes=0; this->unschedule(schedule_selector(TouchTest::updatelongprogress)); //如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行 if (m_longProgress ) { touchCounts=0; m_longProgress=false; return; } m_endPoint=touch->getLocation(); long long endTime=getCurrentTime(); long long timeDis=endTime-m_startTime; E_SWIP_DIR dir=GetSwipDir(m_startPoint, m_endPoint,timeDis); if ( dir != E_INVAILD) { onSwip(m_startPoint, m_endPoint, dir); return; } //做连击判断 if (isMoved) { isMoved=false; return; } if (touchCounts == 2) { onThreeClick(); touchCounts=0; } else if (touchCounts == 1) { this->scheduleOnce(schedule_selector(TouchTest::updateDoubleDelay), 0.25); touchCounts++; } else if (touchCounts == 0) { this->scheduleOnce(schedule_selector(TouchTest::updateSingleDelay), 0.25); touchCounts++; } }
#include <iostream>#include "cocos2d.h"USING_NS_CC;const int minSwipdistance=100;const int minSwiptime=1000; //毫秒const int maxClickedDis=20;enum E_SWIP_DIR{ E_INVAILD, E_LEFT, E_RIGHT, E_UP, E_DOWN};class TouchTest:public Layer{public: TouchTest(); bool touchBegan(Touch * touch,Event * event); void touchMoved(Touch * touch,Event * event); void touchEnded(Touch * touch,Event * event); void touchCancel(Touch * touch,Event * event); void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event); void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event); void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event); void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event); void updateSingleDelay(float); void updateDoubleDelay(float); void updatelongprogress(float); long long getCurrentTime(); void stopSchedule(); private: E_SWIP_DIR GetSwipDir(Point start,Point end,long long timeDis); public: virtual void onSingleCLick(); //单击 virtual void onDoubleClick(); //双击 virtual void onThreeClick(); //3连击 virtual void onLongPressed(); //长按 virtual void onMove(Point); //移动 virtual void onSwip(Point,Point,E_SWIP_DIR); //滑动
0 0
- cocos2dx touch 单击、双击、三连击和长按事件处理
- cocos2dx touch 单击、双击、三连击和长按事件处理
- cocos2dx touch 单击、双击、三连击和长按事件处理
- Cocos2D-X2.2.3学习笔记8(处理精灵单击、双击和三连击事件)
- 键值判断单击双击和长按事件
- View单击双击长按事件
- 鼠标单击和双击中的事件处理
- android处理单击双击和滑动事件
- 单击和双击事件的冲突处理
- 单击和双击事件的冲突处理
- android处理单击双击和滑动事件
- flash 中用单击事件区别处理双击和单击
- Android 横屏启动activity,点击屏幕的单击、双击和长按事件
- Android通过onTouch事件实现单击,双击,长按
- Android view的单击,双击,长按事件监听
- Android通过onTouch事件实现单击,双击,长按
- 区分 touch 事件是单击还是双击
- iOS touch事件单击双击区分响应
- python 的序列方法,元组和列表的方法。
- zookeeper学习
- C++中的const成员函数介绍
- 内存泄漏以及常见的解决方法
- Excel 2010高级应用-饼图(四)
- cocos2dx touch 单击、双击、三连击和长按事件处理
- android: slide 滑动动画效果
- Effective C++——》条款12:复制对象时勿忘其每一部分
- 写给加班的IT女生
- 飞思卡尔(三)测速测正反转(K60 欧姆龙500线编码器DMA测速)
- n皇后问题
- Android大图片裁剪终极解决方案(上:原理分析)
- 软件测试的基因是什么?
- 常见内存泄露及解决方案