cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件

前端之家收集整理的这篇文章主要介绍了cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在CCScrollView中添加ccmenu实现滑动效果是不可能的,因为ccmenu的触发事件是你在touchBegan就全部捕获掉了,如果你想滑动CCScrollView取消选中这个菜单是无法实现的,.所以我们应该自己编写一个模拟ccmenu菜单的控件,在按下按钮后,如果不移动按钮的话,就触发触摸事件,移动的话就取消触摸事件,实现按钮在CCScrollView中的滚动效果。。。

开发人员:Jason's.Alex QQ:531401335

csdn博客:http://blog.csdn.net/RuShrooM


  1. //
  2. //CCButtonSptite.h
  3. //CCSpriteButton
  4. //
  5. //Createdbyjasonsalexon13-8-6.
  6. //
  7. //
  8. #ifndef__CCSpriteButton__CCButtonSptite__
  9. #define__CCSpriteButton__CCButtonSptite__
  10. #include"cocos2d.h"
  11. #defineTOUCH_SENSITIVITY20//触摸灵敏度
  12. usingnamespacecocos2d;
  13. classButtonSprite:publicCCSprite,publicCCTouchDelegate
  14. {
  15. public:
  16. ButtonSprite();
  17. virtualboolinit(CCSpriteFrame*selFile,CCSpriteFrame*disFile);
  18. staticButtonSprite*create(constchar*selFile,constchar*disFile);
  19. staticButtonSprite*createWithSpriteFrame(CCSpriteFrame*selFrame,CCSpriteFrame*disFrame);
  20. staticButtonSprite*createWithSpriteFrameName(constchar*selFile,constchar*disFile);
  21. virtual~ButtonSprite();
  22. virtualvoidonEnter();
  23. virtualvoidonExit();
  24. virtualboolccTouchBegan(CCTouch*touch,CCEvent*event);
  25. virtualvoidccTouchMoved(CCTouch*touch,CCEvent*event);
  26. virtualvoidccTouchEnded(CCTouch*touch,CCEvent*event);
  27. voidregisterScriptTapHandler(intnHandler);
  28. voidunregisterScriptTapHandler(void);
  29. private:
  30. CCSprite*selSprite;//选择的精灵
  31. CCSprite*disSprite;//不选择的精灵
  32. intm_nScriptTapHandler;//脚本函数句柄
  33. boolisEmitTouchEvent;//是否发射触摸事件
  34. CCPointstartTouchPoint;//开始的触摸坐标
  35. };
  36. #endif/*defined(__CCSpriteButton__CCButtonSptite__)*/

  1. //
  2. //CCButtonSptite.cpp
  3. //CCSpriteButton
  4. //
  5. //Createdbyjasonsalexon13-8-6.
  6. //
  7. //
  8. #include"ButtonSprite.h"
  9. ButtonSprite*ButtonSprite::create(constchar*selFile,constchar*disFile)
  10. {
  11. ButtonSprite*pRet=newButtonSprite();
  12. if(pRet&&pRet->init(CCSprite::create(selFile)->displayFrame(),CCSprite::create(disFile)->displayFrame()))
  13. {
  14. pRet->autorelease();
  15. returnpRet;
  16. }
  17. CC_SAFE_DELETE(pRet);
  18. returnNULL;
  19. }
  20. ButtonSprite*ButtonSprite::createWithSpriteFrame(CCSpriteFrame*selFrame,CCSpriteFrame*disFrame)
  21. {
  22. ButtonSprite*pRet=newButtonSprite();
  23. if(pRet&&pRet->init(selFrame,disFrame))
  24. {
  25. pRet->autorelease();
  26. returnpRet;
  27. }
  28. CC_SAFE_DELETE(pRet);
  29. returnNULL;
  30. }
  31. ButtonSprite*ButtonSprite::createWithSpriteFrameName(constchar*selFile,constchar*disFile)
  32. {
  33. ButtonSprite*pRet=newButtonSprite();
  34. CCSpriteFrame*selFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(selFile);
  35. CCSpriteFrame*disFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disFile);
  36. if(pRet&&pRet->init(selFrame,disFrame))
  37. {
  38. pRet->autorelease();
  39. returnpRet;
  40. }
  41. CC_SAFE_DELETE(pRet);
  42. returnNULL;
  43. }
  44. boolButtonSprite::init(CCSpriteFrame*selFrame,CCSpriteFrame*disFrame)
  45. {
  46. this->disSprite=CCSprite::createWithSpriteFrame(disFrame);
  47. this->selSprite=CCSprite::createWithSpriteFrame(selFrame);
  48. this->selSprite->retain();
  49. this->disSprite->retain();
  50. if(!this->initWithSpriteFrame(selFrame))
  51. returnfalse;
  52. returntrue;
  53. }
  54. ButtonSprite::ButtonSprite():m_nScriptTapHandler(0),isEmitTouchEvent(false)
  55. {
  56. }
  57. ButtonSprite::~ButtonSprite()
  58. {
  59. CC_SAFE_DELETE(this->disSprite);
  60. CC_SAFE_DELETE(this->selSprite);
  61. }
  62. voidButtonSprite::onEnter()
  63. {
  64. CCDirector*pDirector=CCDirector::sharedDirector();
  65. pDirector->getTouchDispatcher()->addTargetedDelegate(this,false);
  66. }
  67. voidButtonSprite::onExit()
  68. {
  69. CCDirector*pDirector=CCDirector::sharedDirector();
  70. pDirector->getTouchDispatcher()->removeDelegate(this);
  71. }
  72. boolButtonSprite::ccTouchBegan(CCTouch*touch,CCEvent*event)
  73. {
  74. this->startTouchPoint=convertToNodeSpace(touch->getLocation());
  75. this->isEmitTouchEvent=this->getTextureRect().containsPoint(this->startTouchPoint);
  76. if(isEmitTouchEvent)
  77. {//如果选择了就显示禁用图像
  78. this->setDisplayFrame(disSprite->displayFrame());
  79. returntrue;
  80. }else
  81. {
  82. returnfalse;
  83. }
  84. }
  85. voidButtonSprite::ccTouchMoved(CCTouch*touch,CCEvent*event)
  86. {
  87. floatdistance=this->startTouchPoint.getDistance(convertToNodeSpace(touch->getLocation()));
  88. if(abs(distance)<TOUCH_SENSITIVITY)//判断是否超过了移动范围
  89. {
  90. this->isEmitTouchEvent=true;
  91. }else
  92. {
  93. this->isEmitTouchEvent=false;
  94. }
  95. }
  96. voidButtonSprite::ccTouchEnded(CCTouch*touch,CCEvent*event)
  97. {
  98. if(this->isEmitTouchEvent)
  99. {
  100. CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(this->m_nScriptTapHandler,"end");
  101. }
  102. this->setDisplayFrame(selSprite->displayFrame());//恢复图像
  103. }
  104. voidButtonSprite::registerScriptTapHandler(intnHandler)
  105. {
  106. unregisterScriptTapHandler();
  107. m_nScriptTapHandler=nHandler;
  108. }
  109. voidButtonSprite::unregisterScriptTapHandler(void)
  110. {
  111. if(m_nScriptTapHandler)
  112. {
  113. CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptTapHandler);
  114. m_nScriptTapHandler=0;
  115. }
  116. }
原文链接:https://www.f2er.com/cocos2dx/345406.html

猜你在找的Cocos2d-x相关文章