cocos2dx点击事件的分发问题

前端之家收集整理的这篇文章主要介绍了cocos2dx点击事件的分发问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们工作室需要用cocos2dx来写项目,不过之前的分工有些不是很明确,每个人都使用cocos2dx中的点击事件封装了好几个按钮。所以造成了,游戏的基础框架中出现了N种风格的按钮,于是问题就来了:由于我们使用的cocos2dx中的触摸事件是单点,也就是说非手势的触摸事件。不过cocos2dx在ios和安卓平台上对于单点的响应有些不一致。ios的单点触摸事件的响应机制是,如果屏幕上两个地方同时响应触摸事件,如果一个地方先响应触摸事件,另一个地方的触摸事件就不会再向下分发下去,这样就避免了两个事件都会响应。而安卓中的触摸点击事件分发就显得比较蛋疼了,是一个接一个的分发,最终造成的结果就是如果你用手同时点击两个按钮(尽管同时这种事情在cpu处理看来基本是不可能的),会把这两次触摸事件依次分发下去,这样,如果两次点击事件触发加载两个不同的场景的情况下,就显得比较混乱,所以我们需要做到在两个按钮同时点击的时候肯定要屏蔽到后点击到的按钮的点击事件。

经过一些思索,决定统一写一个基类来搞定这个问题。

  1. #include <stdio.h>
  2. #include "cocos2d.h"
  3. #include "WWCompatibleClasses.h"
  4.  
  5. class WWButton:public cocos2d::Node
  6. {
  7. public:
  8. WWButton();
  9. ~WWButton();
  10. virtual bool init();
  11. virtual bool init(wawa::WWNode* pTouchNode);
  12. virtual void onEnter();
  13. virtual void onExit();
  14.  
  15. virtual bool onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event);
  16. virtual void onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event);
  17. virtual void onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event);
  18. virtual void onTouchCancelled(cocos2d::Touch *touch,cocos2d::Event *unused_event);
  19. virtual void touchActive(cocos2d::Touch* touch){};
  20.  
  21. void setEnabled(bool en);
  22. void setTouchSwallow(bool enable);
  23.  
  24. protected:
  25. CC_SYNTHESIZE(wawa::WWNode*,m_pTouchNode,TouchNode);
  26. CC_SYNTHESIZE(bool,m_hadCancel,HadCancel);
  27.  
  28. private:
  29.  
  30. bool containTouch(cocos2d::Touch* touch);
  31.  
  32. private:
  33. bool m_bEnable;
  34. cocos2d::Vec2 m_beginPoint;
  35. bool m_swallow;
  36. //cocos2d::Vec2 m_movePoint;
  37. };
  38.  
  39. #endif /* defined(__helloworld__WWButton__) */
此类继承了node,所以肯定可以使用最基本的cocos2d的一些UI功能,cpp文件中实现了触摸事件的分发,而这个分发中,我们用一个变量来控制所有触摸事件的接收。也就是当一个按钮被点击的时候,设置一个状态,这个状态属于全局的一个变量,把此状态置为false,然后其他按钮在执行到touch事件的时候会先经过这个状态判断,当状态为false的时候,就touchbegan就会返回false,这个touch事件就无法执行到 onTouchEnded方法,所以这个按钮就不会响应具体的点击事件,因为具体的点击事件都写在 onTouchEnded方法里边.具体实现代码如下:

  1. #include "WWButton.h"
  2. #include "ButtonManager.h"
  3. #include "../WWMacros.h"
  4.  
  5. USING_NS_CC;
  6. USING_NS_WW;
  7.  
  8. namespace ButtonArgument
  9. {
  10. const static float g_moveMax = 10;
  11. }
  12.  
  13. WWButton::WWButton():
  14. m_pTouchNode(nullptr),m_bEnable(true),m_swallow(true),m_hadCancel(false)
  15. {
  16. }
  17.  
  18. WWButton::~WWButton()
  19. {
  20. }
  21.  
  22. bool WWButton::init()
  23. {
  24. if (!Node::init())
  25. {
  26. return false;
  27. }
  28. return true;
  29. }
  30.  
  31. bool WWButton::init(WWNode *pTouchNode)
  32. {
  33. if (!Node::init())
  34. {
  35. return false;
  36. }
  37. this->setCascadeColorEnabled(true);
  38. this->setTouchNode(pTouchNode);
  39. return true;
  40. }
  41.  
  42. void WWButton::onEnter()
  43. {
  44. Node::onEnter();
  45. auto listener = EventListenerTouchOneByOne::create();
  46. listener->setSwallowTouches(m_swallow);
  47.  
  48. listener->onTouchBegan = CC_CALLBACK_2(WWButton::onTouchBegan,this);
  49. listener->onTouchMoved = CC_CALLBACK_2(WWButton::onTouchMoved,this);
  50. listener->onTouchEnded = CC_CALLBACK_2(WWButton::onTouchEnded,this);
  51. listener->onTouchCancelled = CC_CALLBACK_2(WWButton::onTouchCancelled,this);
  52. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
  53. }
  54.  
  55. void WWButton::onExit()
  56. {
  57. Node::onExit();
  58. }
  59.  
  60. bool WWButton::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event)
  61. {
  62. if (!m_bEnable)
  63. {
  64. return false;
  65. }
  66.  
  67. if (!ButtonManager::getInstance()->getIsClickEnable())
  68. {
  69. return false;
  70. }
  71.  
  72. if (!containTouch(touch))
  73. {
  74. return false;
  75. }
  76.  
  77. ButtonManager::getInstance()->setIsClickEnable(false);
  78. this->setColor(Color3B::GRAY);
  79. m_beginPoint = touch->getLocation();
  80. return true;
  81. }
  82.  
  83. void WWButton::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event)
  84. {
  85. if (!m_swallow)
  86. {
  87. auto movePoint = touch->getLocation();
  88. if (abs(movePoint.x-m_beginPoint.x)>ButtonArgument::g_moveMax||abs(movePoint.y-m_beginPoint.y)>ButtonArgument::g_moveMax)
  89. {
  90. onTouchCancelled(touch,unused_event);
  91. }
  92. }
  93. }
  94.  
  95.  
  96. void WWButton::onTouchCancelled(cocos2d::Touch *touch,cocos2d::Event *unused_event)
  97. {
  98. this->setColor(Color3B::WHITE);
  99. ButtonManager::getInstance()->setIsClickEnable(true);
  100. m_hadCancel = true;
  101. }
  102.  
  103. void WWButton::onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event)
  104. {
  105. this->setColor(Color3B::WHITE);
  106. ButtonManager::getInstance()->setIsClickEnable(true);
  107. CCLOG("WWButton::onTouchEnded -------------");
  108. if (!m_hadCancel)
  109. {
  110. touchActive(touch);
  111. }
  112. m_hadCancel = false;
  113. }
  114.  
  115. bool WWButton::containTouch(Touch* touch)
  116. {
  117. if (m_pTouchNode == nullptr)
  118. {
  119. CCLOG("ErrorInfo::WWButton::containTouch touchNode is nullptr");
  120. return false;
  121. }
  122. WWPoint touchLocation = touch->getLocation();
  123. WWPoint local = m_pTouchNode->convertToNodeSpace(touchLocation);
  124. WWRect r = m_pTouchNode->getBoundingBox();
  125. r.origin = Vec2::ZERO;
  126. return r.containsPoint(local);
  127. }
  128.  
  129. void WWButton::setEnabled(bool en)
  130. {
  131. m_bEnable = en;
  132. if (m_bEnable)
  133. {
  134. this->setColor(Color3B::WHITE);
  135. }
  136. else
  137. {
  138. this->setColor(Color3B::GRAY);
  139. }
  140. }
  141.  
  142. void WWButton::setTouchSwallow(bool enable)
  143. {
  144. m_swallow = enable;
  145. Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
  146. auto listener = EventListenerTouchOneByOne::create();
  147. listener->setSwallowTouches(enable);
  148. listener->onTouchBegan = CC_CALLBACK_2(WWButton::onTouchBegan,this);
  149. listener->onTouchMoved = CC_CALLBACK_2(WWButton::onTouchMoved,this);
  150. listener->onTouchEnded = CC_CALLBACK_2(WWButton::onTouchEnded,this);
  151. listener->onTouchCancelled = CC_CALLBACK_2(WWButton::onTouchCancelled,this);
  152. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
  153. }

其中ButtonManager是个单例类,存储了这个按钮的状态,也许有人会有疑问,简单的写一个static 变量不就可以了嘛,干嘛还要用单例仅仅为了存储一个变量,额,这就是一个历史问题了,当初我的单例里边是这样的

  1. #ifndef __helloworld__ButtonManager__
  2. #define __helloworld__ButtonManager__
  3.  
  4. #include <stdio.h>
  5. #include "cocos2d.h"
  6. #include "WWButton.h"
  7.  
  8. const static std::string g_btnCreateNotafication = "buttonCreate";
  9. const static std::string g_buttonClickNotafication = "buttonClick";
  10. const static std::string g_buttonReset = "buttonReset";
  11. const static std::string g_buttonRemove = "button_Remove";
  12. const static std::string g_sceneChange = "scene_change";
  13.  
  14. class ButtonManager;
  15. static ButtonManager* m_instance = nullptr;
  16. class ButtonManager
  17. {
  18. public:
  19. ButtonManager();
  20. ~ButtonManager();
  21. static ButtonManager* getInstance()
  22. {
  23. if (!m_instance)
  24. {
  25. m_instance = new ButtonManager();
  26. }
  27. return m_instance;
  28. }
  29. static void release()
  30. {
  31. if (m_instance)
  32. {
  33. delete m_instance;
  34. m_instance = nullptr;
  35. }
  36. }
  37. void beginListen();
  38. private:
  39.  
  40. CC_SYNTHESIZE(bool,m_bIsClickEnable,IsClickEnable);
  41. private:
  42. std::vector<WWButton*> m_allButtons;
  43. };
  44. #endif /* defined(__helloworld__ButtonManager__) */


  1. #include "ButtonManager.h"
  2. USING_NS_CC;
  3. #include "WWTouchSprite.h"
  4. #include "WWTwoPicBtn.h"
  5.  
  6. ButtonManager::ButtonManager():
  7. m_bIsClickEnable(true)
  8. {
  9. }
  10.  
  11. ButtonManager::~ButtonManager()
  12. {
  13. }
  14.  
  15. void ButtonManager::beginListen()
  16. {
  17. // /*******监听按钮的创建********************************************/
  18. // Director::getInstance()->getEventDispatcher()->addCustomEventListener(g_btnCreateNotafication,[&](EventCustom* event)
  19. //{
  20. // auto btn = (WWButton*)event->getUserData();
  21. // if (btn)
  22. // {
  23. // btn->setBtnIndex((int)m_allButtons.size());
  24. // m_allButtons.push_back(btn);
  25. // CCLOG("the m_allButton size=%ld",m_allButtons.size());
  26. // CCLOG("the beginListen() btn idx=%ld",btn->getBtnIndex());
  27. // }
  28. //});
  29.  
  30. //
  31. //
  32. //
  33. // /*******监听按钮点击事件(touchbegan)********************************************/
  34. // Director::getInstance()->getEventDispatcher()->addCustomEventListener(g_buttonClickNotafication,[&](EventCustom* event)
  35. //{
  36. // auto btn = (WWButton*)event->getUserData();
  37. // CCLOG("the btn indx =%ld",btn->getBtnIndex());
  38. // if (btn)
  39. // {
  40. // for (auto obj:m_allButtons)
  41. // {
  42. // CCLOG("the every obj inx=%ld",obj->getBtnIndex());
  43. // if (btn->getBtnIndex()!=obj->getBtnIndex())
  44. // {
  45. // CCLOG("obj index%ldd",obj->getBtnIndex());
  46. // obj->setTouchEnable(false);
  47. // }
  48. // }
  49. // }
  50. //});
  51. //
  52. //
  53. //
  54. // /*******所有按钮置回可点状态********************************************/
  55. // Director::getInstance()->getEventDispatcher()->addCustomEventListener(g_buttonReset,[&](EventCustom* event)
  56. // {
  57. // for (auto obj:m_allButtons)
  58. // {
  59. // obj->setTouchEnable(true);
  60. // }
  61. // });
  62. //
  63. //
  64. //
  65. // /*******按钮移除通知监听********************************************/
  66. // Director::getInstance()->getEventDispatcher()->addCustomEventListener(g_buttonRemove,[&](EventCustom* event)
  67. //{
  68. // auto btn = (WWButton*)event->getUserData();
  69. // if (btn)
  70. // {
  71. // std::vector<WWButton*>::iterator i = m_allButtons.begin();
  72. // while (i!=m_allButtons.end())
  73. // {
  74. // WWButton* obj = *i;
  75. // if (obj->getBtnIndex()==btn->getBtnIndex())
  76. // {
  77. // m_allButtons.erase(i);
  78. // break;
  79. // }
  80. // i++;
  81. // }
  82. // }
  83. //});
  84. }

也就是说,一开始我把所有的按钮单独存储了一个状态,每次按钮点击的时候就会发送通知,将所有的按钮状态均置为fasle,不过后来幡然悔悟,用个全局变量解决所有问题了,然后开始敲脑袋觉得智商捉急啊,所以把一些多此一举的代码贴上,以表耻辱。(不过还是觉得自己之前的想法也是有那么一点用处的,万一之后扩展,需要每次改变一些按钮的状态,这个按钮管理还是用得着的嘛。诸位莫要拍砖)

所以综上所述。cocos2dx有一点封装的还是不够好,就是在按钮点击方面,需要创建之后加入一个Menu里边,而如果一个界面出现多个层次的按钮,就需要多个Menu,而多个Menu之间再去做互斥又是多么蛋疼的事情啊,所以还是封装一个按钮来做互斥比较好一些。(今天喝了点白酒+龙舌兰+伏特加)半醉半朦胧,若有语焉不详之处,或者愚蠢的地方,欢迎留言拍砖。

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