cocos2dx touch 单击、双击、三连击和长按事件处理
来源:互联网 发布:幼儿园床垫淘宝 编辑:程序博客网 时间:2024/05/19 04:03
- #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();
- virtual void onLongPressed();
- virtual void onMove(Point);
- virtual void onSwip(Point,Point,E_SWIP_DIR);
0 0