#include "cocos2d.h"
USING_NS_CC;
class LongTouch :
public cocos2d::CCNode, public cocos2d::CCTouchDelegate
{
CC_SYNTHESIZE(CCSprite*, m_normalSprite, NormalSprite);
CC_SYNTHESIZE(CCSprite*, m_shortTouchSprite, ShortTouchSprite);
CC_SYNTHESIZE(CCSprite*, m_longTouchSprite, LongTouchSprite);
public:
LongTouch(void);
~LongTouch(void);
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
bool initWithNormalImage(const char *normalImage,const char *shortTouchImage,const char *longTouchImage,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort);
bool initWithNormalSprite(CCSprite *normalSprite,CCSprite *shortTouchSprite,CCSprite *longTouchSprite,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort);
static LongTouch* create( const char *normalImage,const char *shortTouchImage,const char *longTouchImage,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort );
void updateTime(float dt);
protected:
virtual bool init();
cocos2d::CCObject* m_callTarget;
cocos2d::SEL_MenuHandler m_callLongTouch;
cocos2d::SEL_MenuHandler m_callShortTouch;
private:
float timer;
bool m_touched;
};
—————————————————————————————————————————————————————————————————————————————
using namespace cocos2d;
LongTouch::LongTouch(void)
:m_callTarget(0)
{
}
LongTouch::~LongTouch(void)
{
}
bool LongTouch::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
cocos2d::CCPoint p=pTouch->getLocation();
if (m_normalSprite)
{
p = m_normalSprite->convertToNodeSpaceAR(p);
if (m_normalSprite->boundingBox().containsPoint(p))
{
m_touched = true;
}
}
return true;
}
void LongTouch::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
cocos2d::CCPoint p=pTouch->getLocation();
if (m_normalSprite)
{
p = m_normalSprite->convertToNodeSpaceAR(p);
if (m_normalSprite->boundingBox().containsPoint(p))
{
m_touched = true;
}
else
{
m_touched = false;
timer = 0;
}
}
}
void LongTouch::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
cocos2d::CCPoint p=pTouch->getLocation();
if (m_normalSprite)
{
p = m_normalSprite->convertToNodeSpaceAR(p);
if (m_normalSprite->boundingBox().containsPoint(p))
{
if(timer>=0.5f)
{
if( m_callTarget )
{
((*m_callTarget).*m_callLongTouch)(this);
}
}
if(timer<0.5f)
{
if( m_callTarget )
{
((*m_callTarget).*m_callShortTouch)(this);
}
}
}
}
timer = 0;
m_touched = false;
m_normalSprite->setVisible(true);
m_shortTouchSprite->setVisible(false);
m_longTouchSprite->setVisible(false);
}
void LongTouch::updateTime(float dt)
{
if(m_touched)
{//不要用unschedule
timer += dt;
if(timer>=0.5f)
{
m_normalSprite->setVisible(false);
m_shortTouchSprite->setVisible(false);
m_longTouchSprite->setVisible(true);
}
else
{
m_normalSprite->setVisible(false);
m_shortTouchSprite->setVisible(true);
m_longTouchSprite->setVisible(false);
}
}
}
bool LongTouch::init()
{
cocos2d::CCTouchDispatcher* td = cocos2d::CCDirector::sharedDirector()->getTouchDispatcher();
td->addTargetedDelegate( this, 0, true );
this->schedule(schedule_selector( LongTouch::updateTime));
timer = 0;
m_touched = false;
return true;
}
LongTouch* LongTouch::create( const char *normalImage,const char *shortTouchImage,const char *longTouchImage,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort)
{
LongTouch* s = new LongTouch();
if(s->init())
{
s->initWithNormalImage(normalImage,shortTouchImage,longTouchImage,target, callLong,callShort);
s->autorelease();//自动释放
}
return s;
}
bool LongTouch::initWithNormalImage(const char *normalImage,const char *shortTouchImage,const char *longTouchImage,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort)
{
m_normalSprite = NULL;
m_shortTouchSprite = NULL;
m_longTouchSprite = NULL;
if (normalImage)
{
m_normalSprite = CCSprite::create(normalImage);
m_normalSprite->setVisible(true);
addChild(m_normalSprite);
}
if (shortTouchImage)
{
m_shortTouchSprite = CCSprite::create(shortTouchImage);
m_shortTouchSprite->setVisible(false);
addChild(m_shortTouchSprite);
}
if(longTouchImage)
{
m_longTouchSprite = CCSprite::create(longTouchImage);
m_longTouchSprite->setVisible(false);
addChild(m_longTouchSprite);
}
return initWithNormalSprite(m_normalSprite, m_shortTouchSprite, m_longTouchSprite, target,callLong,callShort);
}
bool LongTouch::initWithNormalSprite(CCSprite *normalSprite,CCSprite *shortTouchSprite,CCSprite *longTouchSprite,cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callLong,cocos2d::SEL_MenuHandler callShort)
{
m_callLongTouch = callLong;
m_callShortTouch = callShort;
m_callTarget = target;
setNormalSprite(m_normalSprite);
setShortTouchSprite(m_shortTouchSprite);
setLongTouchSprite(m_longTouchSprite);
return true;
}