在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__
- #define__HELLOWORLD_SCENE_H__
- #include"cocos2d.h"
- #include"cocos-ext.h"
- usingnamespacestd;
- usingnamespacecocos2d;
- usingnamespaceextension;
- enum{
- backgroundTag=0,
- };
- classHelloWorld:publicCCLayer
- {
- public:
- virtualboolinit();
- staticCCScene*scene();
- CCSprite*red_sp;
- CCSprite*green_sp;
- CCSprite*blue_sp;
- CCSprite*longTouch_sp;
- voidupdate();
- virtualboolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidccTouchEnded(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent);
- virtualvoidonEnter();
- virtualvoidonExit();
- CREATE_FUNC(HelloWorld);
- };
- #endif//__HELLOWORLD_SCENE_H__
.cpp文件
- #include"HelloWorldScene.h"
- #include"SimpleAudioEngine.h"
- usingnamespacecocos2d;
- usingnamespaceCocosDenshion;
- CCScene*HelloWorld::scene()
- {
- CCScene*scene=CCScene::create();
- HelloWorld*layer=HelloWorld::create();
- scene->addChild(layer);
- returnscene;
- }
- boolHelloWorld::init()
- {
- if(!CCLayer::init())
- {
- returnfalse;
- }
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- //添加一背景当点击此背景时弹出键盘
- CCSprite*background=CCSprite::create("HelloWorld.png");
- background->setScale(2);
- background->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(background,1,backgroundTag);
- //红色精灵
- red_sp=CCSprite::create("Icon.png");
- red_sp->setColor(ccRED);
- red_sp->setPosition(ccp(size.width*0.3,size.height*0.5));
- this->addChild(red_sp,1);
- //绿色精灵
- green_sp=CCSprite::create("Icon.png");
- green_sp->setColor(ccGREEN);
- green_sp->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(green_sp,2);
- //蓝色精灵
- blue_sp=CCSprite::create("Icon.png");
- blue_sp->setColor(ccBLUE);
- blue_sp->setPosition(ccp(size.width*0.7,size.height*0.5));
- this->addChild(blue_sp,3);
- returntrue;
- }
- voidHelloWorld::update()
- {
- this->unschedule(schedule_selector(HelloWorld::update));
- //被长按触发的精灵做旋转动作
- longTouch_sp->runAction(CCRotateBy::create(1,360));
- }
- boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
- {
- CCPointtouch_point=pTouch->getLocation();
- if(red_sp->boundingBox().containsPoint(touch_point)){
- this->schedule(schedule_selector(HelloWorld::update),1);
- longTouch_sp=red_sp;
- }elseif(green_sp->boundingBox().containsPoint(touch_point)){
- this->schedule(schedule_selector(HelloWorld::update),1);
- longTouch_sp=green_sp;
- }elseif(blue_sp->boundingBox().containsPoint(touch_point)){
- this->schedule(schedule_selector(HelloWorld::update),1);
- longTouch_sp=blue_sp;
- }
- returntrue;
- }
- voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
- {
- CCPointtouch_point=pTouch->getLocation();
- if(longTouch_sp){
- //当手指滑动超出所点精灵范围时取消触发
- if(!longTouch_sp->boundingBox().containsPoint(touch_point)){
- this->unschedule(schedule_selector(HelloWorld::update));
- }
- }
- }
- voidHelloWorld::ccTouchEnded(CCTouch*pTouch,CCEvent*pEvent)
- {
- this->unschedule(schedule_selector(HelloWorld::update));
- }
- voidHelloWorld::ccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent){}
- voidHelloWorld::onEnter()
- {
- //事件注册
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);
- CCLayer::onEnter();
- }
- voidHelloWorld::onExit()
- {
- CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
- CCLayer::onExit();
- }
效果如下图,当长按某一精灵达到1秒时,就会触发旋转动作。