在cocos2dx中给我们提供了很多点击屏幕触发的事件监听,比如CCMenu类的一系列 ,ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent),ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent), ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent),ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent)等监听方法,但有时项目中会要求当长按某一精灵达到一定时间时才响应触发事件,而不是简单的按下抬起,这时就可以利用schedule来实现,下面是实现代码。
.h文件
- #ifndef__HELLOWORLD_SCENE_H__ @H_404_50@#define__HELLOWORLD_SCENE_H__
- @H_404_50@#include"cocos2d.h"
- #include"cocos-ext.h" @H_404_50@
- usingnamespacestd; @H_404_50@usingnamespacecocos2d;
- usingnamespaceextension; @H_404_50@
- enum{ @H_404_50@backgroundTag=0,
- }; @H_404_50@
- classHelloWorld:publicCCLayer @H_404_50@{
- public: @H_404_50@virtualboolinit();
- staticCCScene*scene(); @H_404_50@
- CCSprite*red_sp; @H_404_50@CCSprite*green_sp;
- CCSprite*blue_sp; @H_404_50@CCSprite*longTouch_sp;
- @H_404_50@voidupdate();
- @H_404_50@virtualboolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent); @H_404_50@virtualvoidccTouchEnded(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent); @H_404_50@virtualvoidonEnter();
- virtualvoidonExit(); @H_404_50@
- CREATE_FUNC(HelloWorld); @H_404_50@};
- @H_404_50@#endif//__HELLOWORLD_SCENE_H__
.cpp文件
- #include"HelloWorldScene.h" @H_404_50@#include"SimpleAudioEngine.h"
- @H_404_50@usingnamespacecocos2d;
- usingnamespaceCocosDenshion; @H_404_50@
- CCScene*HelloWorld::scene() @H_404_50@{
- CCScene*scene=CCScene::create(); @H_404_50@HelloWorld*layer=HelloWorld::create();
- scene->addChild(layer); @H_404_50@returnscene;
- } @H_404_50@
- boolHelloWorld::init() @H_404_50@{
- if(!CCLayer::init()) @H_404_50@{
- returnfalse; @H_404_50@}
- @H_404_50@CCSizesize=CCDirector::sharedDirector()->getWinSize();
- @H_404_50@//添加一背景当点击此背景时弹出键盘
- CCSprite*background=CCSprite::create("HelloWorld.png"); @H_404_50@background->setScale(2);
- background->setPosition(ccp(size.width*0.5,size.height*0.5)); @H_404_50@this->addChild(background,1,backgroundTag);
- @H_404_50@//红色精灵
- red_sp=CCSprite::create("Icon.png"); @H_404_50@red_sp->setColor(ccRED);
- red_sp->setPosition(ccp(size.width*0.3,size.height*0.5)); @H_404_50@this->addChild(red_sp,1);
- //绿色精灵 @H_404_50@green_sp=CCSprite::create("Icon.png");
- green_sp->setColor(ccGREEN); @H_404_50@green_sp->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(green_sp,2); @H_404_50@//蓝色精灵
- blue_sp=CCSprite::create("Icon.png"); @H_404_50@blue_sp->setColor(ccBLUE);
- blue_sp->setPosition(ccp(size.width*0.7,size.height*0.5)); @H_404_50@this->addChild(blue_sp,3);
- @H_404_50@returntrue;
- } @H_404_50@
- voidHelloWorld::update() @H_404_50@{
- this->unschedule(schedule_selector(HelloWorld::update)); @H_404_50@//被长按触发的精灵做旋转动作
- longTouch_sp->runAction(CCRotateBy::create(1,360)); @H_404_50@}
- @H_404_50@boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
- { @H_404_50@CCPointtouch_point=pTouch->getLocation();
- if(red_sp->boundingBox().containsPoint(touch_point)){ @H_404_50@this->schedule(schedule_selector(HelloWorld::update),1);
- longTouch_sp=red_sp; @H_404_50@}elseif(green_sp->boundingBox().containsPoint(touch_point)){
- this->schedule(schedule_selector(HelloWorld::update),1); @H_404_50@longTouch_sp=green_sp;
- }elseif(blue_sp->boundingBox().containsPoint(touch_point)){ @H_404_50@this->schedule(schedule_selector(HelloWorld::update),1);
- longTouch_sp=blue_sp; @H_404_50@}
- returntrue; @H_404_50@}
- @H_404_50@voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
- { @H_404_50@CCPointtouch_point=pTouch->getLocation();
- if(longTouch_sp){ @H_404_50@//当手指滑动超出所点精灵范围时取消触发
- if(!longTouch_sp->boundingBox().containsPoint(touch_point)){ @H_404_50@this->unschedule(schedule_selector(HelloWorld::update));
- } @H_404_50@}
- } @H_404_50@
- voidHelloWorld::ccTouchEnded(CCTouch*pTouch,CCEvent*pEvent) @H_404_50@{
- this->unschedule(schedule_selector(HelloWorld::update)); @H_404_50@}
- @H_404_50@voidHelloWorld::ccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent){}
- @H_404_50@
- voidHelloWorld::onEnter() @H_404_50@{
- //事件注册 @H_404_50@CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);
- CCLayer::onEnter(); @H_404_50@}
- voidHelloWorld::onExit() @H_404_50@{
- CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); @H_404_50@CCLayer::onExit();
- }
效果如下图,当长按某一精灵达到1秒时,就会触发旋转动作。