长按键方法实现
1、首先开启触摸
记着在init方法中开启触摸:this->setTouchEnabled(true);
2、在.h文件中声明或重写以下方法
重写几个方法
//------------------------------------.h文件--------------------------------------------------
//触摸方法
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void onEnter();
virtual void onExit();
//长按键计时器方法-------加-----------------
void update_jia_func(float dlt);
//长按键计时器方法-------减-----------------
void update_jian_func(float dlt);
//------------------------------------.h文件--------------------------------------------------
3、
该例子中实际上包含了两个长按键功能的实现:
分别是和,都实现了长按键的功能
以下是方法中的具体内容:
void ChangeMoney::onEnter()
{
CCLayer::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
void ChangeMoney::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
//1.在ccTouchBegan和ccTouchMoved中实现长按键处理
bool ChangeMoney::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint p=pTouch->getLocation();
//首先判断是否点中了加号
//获取加号的tag值
sprite=(CCSprite *)this->getChildByTag(101);
if (sprite)
{
if (sprite->boundingBox().containsPoint(p))
{
//currentCount是一个int型变量,标识数字的当前值,allCanBeChanged是最大值,判断currentCount是否已经增加到最多
if (currentCount>=allCanBeChanged)
{
this->unschedule(schedule_selector(ChangeMoney::update_jia_func));
//设置图片为灰色,灰色标示不可用
sprite=(CCSprite *)this->getChildByTag(101);
if (sprite)sprite->setColor(ccGRAY);
}
else
{
//设置图片为白色
sprite=(CCSprite *)this->getChildByTag(101);
if (sprite)sprite->setColor(ccWHITE);
//currentCount没有增加到最大值时,允许自增
currentCount++;
//currentCount增加后改变label的显示
CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(201);
string=CCString::createWithFormat("%d",currentCount);
label->setString(string->getCString());
}
//当touchBegin时,先是currentCount自增1,然后再执行计时器函数
this->schedule(schedule_selector(ChangeMoney::update_jia_func),0.1f);
}
//当触摸到sprite之外时停止计时器函数
else
{
this->unschedule(schedule_selector(ChangeMoney::update_jia_func));
}
}
//-----------------减操作的原理与上面加操作的原理一样---------------------------------------------
//接着判断是否点中了减号
//根据tag获取sprite
sprite=(CCSprite *)this->getChildByTag(102);
if (sprite)
{
if (sprite->boundingBox().containsPoint(p))
{
//已经减少至最少
if (currentCount<=0)
{
//停止计时器函数,因为已经减少到最少了,没必要再执行计时器函数消耗系统资源了
this->unschedule(schedule_selector(ChangeMoney::update_jian_func));
//设置图片为灰色,灰色默认为不可用
sprite=(CCSprite *)this->getChildByTag(102);
if (sprite)sprite->setColor(ccGRAY);
}
//没有减少至最少,可执行减的操作
else
{
//设置图片为白色
sprite=(CCSprite *)this->getChildByTag(102);
if (sprite)sprite->setColor(ccWHITE);
//自减1
currentCount--;
//改变label的显示
CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(201);
string=CCString::createWithFormat("%d",currentCount);
label->setString(string->getCString());
}
//调用减号的计时器方法
this->schedule(schedule_selector(ChangeMoney::update_jian_func),0.1f);
}
//没有点中减号则取消计时器函数
else
{
this->unschedule(schedule_selector(ChangeMoney::update_jian_func));
}
}
return true;
}
//ccTouchMoved中方法的执行道理其实跟ccTouchBegan中是一样的
void ChangeMoney::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint p=pTouch->getLocation();
//加
sprite=(CCSprite *)this->getChildByTag(101);
if (sprite)
{
if (sprite->boundingBox().containsPoint(p))
{
this->schedule(schedule_selector(ChangeMoney::update_jia_func),0.1f);
}
else this->unschedule(schedule_selector(ChangeMoney::update_jia_func));
}
//减
sprite=(CCSprite *)this->getChildByTag(102);
if (sprite)
{
//move的过程中划中减号则执行计时器函数自减
if (sprite->boundingBox().containsPoint(p))
{
this->schedule(schedule_selector(ChangeMoney::update_jian_func),0.1f);
}
else this->unschedule(schedule_selector(ChangeMoney::update_jian_func));
}
}
//在ccTouchEnded函数中取消计时器函数的执行
void ChangeMoney::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
this->unscheduleAllSelectors();
}
void ChangeMoney::update_jia_func(float dlt)
{
//已经增加到最多
if (currentCount>=allCanBeChanged)
{
this->unschedule(schedule_selector(ChangeMoney::update_jia_func));
}
else
{
//自增
currentCount++;
//改变label显示
CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(201);
string=CCString::createWithFormat("%d",currentCount);
label->setString(string->getCString());
}
}
void ChangeMoney::update_jian_func(float dlt)
{
//已减少到最少
if (currentCount<=0)
{
this->unschedule(schedule_selector(ChangeMoney::update_jian_func));
}
else
{
//自减
currentCount--;
//改变label显示
CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(201);
string=CCString::createWithFormat("%d",currentCount);
label->setString(string->getCString());
}
4、写到这里基本上已经结束了,给大家大概说下原理当我们点中或时,分几种情况分别是:单击(点一下马上松开)、点击按下持续一段时间、划过某精灵并在精灵上停止一段时间,我们就是在这3种情况下做方法处理,后两种情况就是我们要处理的长按键处理,在处理之前要判断是否点中要执行长按键事件的精灵,如果点中并长按,则开始执行长按键操作。