您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx实现精灵单击、双击、三击级拖拽分别处理

2014-07-01 13:58 381 查看
头文件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");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2dx