cocos2dx 利用schedule实现长按触发事件功能

在cocos2dx中给我们提供了很多点击屏幕触发的事件监听,比如CCMenu类的一系列 ,ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent) ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent)等监听方法,但有时项目中会要求当长按某一精灵达到一定时间时才响应触发事件,而不是简单的按下抬起,这时就可以利用schedule来实现,下面是实现代码


.h文件
  1. #ifndef__HELLOWORLD_SCENE_H__
  2. #define__HELLOWORLD_SCENE_H__
  3. #include"cocos2d.h"
  4. #include"cocos-ext.h"
  5. usingnamespacestd;
  6. usingnamespacecocos2d;
  7. usingnamespaceextension;
  8. enum{
  9. backgroundTag=0,
  10. };
  11. classHelloWorld:publicCCLayer
  12. {
  13. public:
  14. virtualboolinit();
  15. staticCCScene*scene();
  16. CCSprite*red_sp;
  17. CCSprite*green_sp;
  18. CCSprite*blue_sp;
  19. CCSprite*longTouch_sp;
  20. voidupdate();
  21. virtualboolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
  22. virtualvoidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
  23. virtualvoidccTouchEnded(CCTouch*pTouch,CCEvent*pEvent);
  24. virtualvoidccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent);
  25. virtualvoidonEnter();
  26. virtualvoidonExit();
  27. CREATE_FUNC(HelloWorld);
  28. };
  29. #endif//__HELLOWORLD_SCENE_H__

.cpp文件
  1. #include"HelloWorldScene.h"
  2. #include"SimpleAudioEngine.h"
  3. usingnamespacecocos2d;
  4. usingnamespaceCocosDenshion;
  5. CCScene*HelloWorld::scene()
  6. {
  7. CCScene*scene=CCScene::create();
  8. HelloWorld*layer=HelloWorld::create();
  9. scene->addChild(layer);
  10. returnscene;
  11. }
  12. boolHelloWorld::init()
  13. {
  14. if(!CCLayer::init())
  15. {
  16. returnfalse;
  17. }
  18. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  19. //添加一背景当点击此背景时弹出键盘
  20. CCSprite*background=CCSprite::create("HelloWorld.png");
  21. background->setScale(2);
  22. background->setPosition(ccp(size.width*0.5,size.height*0.5));
  23. this->addChild(background,1,backgroundTag);
  24. //红色精灵
  25. red_sp=CCSprite::create("Icon.png");
  26. red_sp->setColor(ccRED);
  27. red_sp->setPosition(ccp(size.width*0.3,size.height*0.5));
  28. this->addChild(red_sp,1);
  29. //绿色精灵
  30. green_sp=CCSprite::create("Icon.png");
  31. green_sp->setColor(ccGREEN);
  32. green_sp->setPosition(ccp(size.width*0.5,size.height*0.5));
  33. this->addChild(green_sp,2);
  34. //蓝色精灵
  35. blue_sp=CCSprite::create("Icon.png");
  36. blue_sp->setColor(ccBLUE);
  37. blue_sp->setPosition(ccp(size.width*0.7,size.height*0.5));
  38. this->addChild(blue_sp,3);
  39. returntrue;
  40. }
  41. voidHelloWorld::update()
  42. {
  43. this->unschedule(schedule_selector(HelloWorld::update));
  44. //被长按触发的精灵做旋转动作
  45. longTouch_sp->runAction(CCRotateBy::create(1,360));
  46. }
  47. boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
  48. {
  49. CCPointtouch_point=pTouch->getLocation();
  50. if(red_sp->boundingBox().containsPoint(touch_point)){
  51. this->schedule(schedule_selector(HelloWorld::update),1);
  52. longTouch_sp=red_sp;
  53. }elseif(green_sp->boundingBox().containsPoint(touch_point)){
  54. this->schedule(schedule_selector(HelloWorld::update),1);
  55. longTouch_sp=green_sp;
  56. }elseif(blue_sp->boundingBox().containsPoint(touch_point)){
  57. this->schedule(schedule_selector(HelloWorld::update),1);
  58. longTouch_sp=blue_sp;
  59. }
  60. returntrue;
  61. }
  62. voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
  63. {
  64. CCPointtouch_point=pTouch->getLocation();
  65. if(longTouch_sp){
  66. //当手指滑动超出所点精灵范围时取消触发
  67. if(!longTouch_sp->boundingBox().containsPoint(touch_point)){
  68. this->unschedule(schedule_selector(HelloWorld::update));
  69. }
  70. }
  71. }
  72. voidHelloWorld::ccTouchEnded(CCTouch*pTouch,CCEvent*pEvent)
  73. {
  74. this->unschedule(schedule_selector(HelloWorld::update));
  75. }
  76. voidHelloWorld::ccTouchCancelled(CCTouch*pTouch,CCEvent*pEvent){}
  77. voidHelloWorld::onEnter()
  78. {
  79. //事件注册
  80. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);
  81. CCLayer::onEnter();
  82. }
  83. voidHelloWorld::onExit()
  84. {
  85. CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
  86. CCLayer::onExit();
  87. }

效果如下图,当长按某一精灵达到1秒时,就会触发旋转动作。

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...