1. 程式人生 > >cocos2dx實現精靈單擊、雙擊、三擊級拖拽分別處理

cocos2dx實現精靈單擊、雙擊、三擊級拖拽分別處理

標頭檔案TouchTest.h

#include <iostream>  
#include "cocos2d.h"    
  
  
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 cocos2d::CCLayerColor 
{  
public:    
    static cocos2d::CCScene* scene();
bool init();
CREATE_FUNC(TouchTest);
void registerWithTouchDispatcher();
    bool ccTouchBegan(cocos2d::CCTouch * touch,cocos2d::CCEvent * event);  
    void ccTouchMoved(cocos2d::CCTouch * touch,cocos2d::CCEvent * event);  
    void ccTouchEnded(cocos2d::CCTouch * touch,cocos2d::CCEvent * event);  
    void ccTouchCancel(cocos2d::CCTouch * touch,cocos2d::CCEvent * event);   
      
      
    void updateSingleDelay(float);  
    void updateDoubleDelay(float);  
  
    void updatelongprogress(float);  
      
    long long getCurrentTime();  
  
      
    void stopSchedule();  
      
private:  
bool isTouch;    
bool isMoved;  
int pressTimes;   
int touchCounts;  
bool m_longProgress;
cocos2d::CCPoint m_startPoint;
cocos2d::CCPoint m_endPoint;
cocos2d::CCSprite* tempsprite;
long long m_startTime;
    E_SWIP_DIR GetSwipDir(cocos2d::CCPoint start,cocos2d::CCPoint end,long long timeDis);  
      
public:  
    virtual void onSingleCLick();        //單擊  
    virtual void onDoubleClick();        //雙擊  
    virtual void onThreeClick();         //3連擊  
    virtual void onLongPressed();        //長按  
    virtual void onMove(cocos2d::CCPoint);          //移動  
    virtual void onSwip(cocos2d::CCPoint,cocos2d::CCPoint,E_SWIP_DIR);    //滑動  };

原始檔TouchTest.cpp

#include "TouchTest.h"  

USING_NS_CC;

CCScene* TouchTest::scene()
{
CCScene* scene=CCScene::create();
CCLayer* layer=TouchTest::create();
scene->addChild(layer);
return scene;
}
bool TouchTest::init()
{
if(!CCLayerColor::initWithColor(ccc4(255,255,255,255)))
{
return false;
}
isTouch=false;  
isMoved=false;  
pressTimes=0;  
touchCounts=0; 
m_longProgress=false;  
setTouchEnabled(true);
tempsprite=CCSprite::create("findbook/book1.png");
tempsprite->setPosition(ccp(120,160));
this->addChild(tempsprite);
return true;
}
void TouchTest::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -126,true); 
}
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::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
{  
CCLog("ccTouchBegan");
m_startPoint=touch->getLocation();  


isTouch=true;  


m_startTime=getCurrentTime();  


//處理長按事件  
this->schedule(schedule_selector(TouchTest::updatelongprogress),1);  


return true;  
}  


void TouchTest::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
{  
CCLog("ccTouchMoved");
isMoved=true;  
CCPoint curPoint=touch->getLocation();
onMove(curPoint);  


}  


void TouchTest::ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
{  
CCLog("ccTouchEnded");
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) {  
CCLog("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++;  
}  
}  
void TouchTest::onDoubleClick()
{
CCLog("onDoubleClick");
}
void TouchTest::onSingleCLick()
{
CCLog("onSingleCLick");
}
void TouchTest::onLongPressed()
{
CCLog("onLongPressed");
}
void TouchTest::onMove(CCPoint point)
{
CCLog("onMove");
tempsprite->setPosition(point);
CCLog("isMoved:%d",isMoved);
}
void TouchTest::onSwip(cocos2d::CCPoint point1,cocos2d::CCPoint point2,E_SWIP_DIR fsd)
{
CCLog("onSwip");
}
void TouchTest::onThreeClick()
{
CCLog("onThreeClick");
}