首页 > 代码库 > 创建一个弹出层

创建一个弹出层


  1. /* 功能
  2. * 1. 弹出层的基类, 比下层拥有更高的优先级, 用于屏蔽下层,及本层外触摸事件
  3. * 2. 提供一个容量,及相应方法,用于装纳需要处理事件的对象 CCTouchDelegate*对象,
  4. */
  5. #ifndef __MythLeague__UpperLayer__
  6. #define __MythLeague__UpperLayer__
  7. #include "Global/Constants.h"
  8. #include "CommonLayer/TouchLogicLayer.h"
  9. class UpperLayer : public TouchLogicLayer{
  10. public:
  11. UpperLayer();
  12. virtual ~UpperLayer();
  13. bool init();
  14. void onEnter();
  15. void onExit();
  16. void registerWithTouchDispatcher();
  17. void appendToTouchDispatch(cocos2d::CCTouchDelegate* p_touchableObject);
  18. #pragma mark 触摸相关
  19. //开始
  20. bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
  21. //移动
  22. void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
  23. //结束
  24. void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
  25. //点击home键或其它方式引起的取消
  26. void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
  27. public:
  28. //cocos2d::CCArray* m_touchDispatchTable;
  29. std::vector<cocos2d::CCTouchDelegate*> m_touchDispatchTable;
  30. int m_iIndex;//m_iIndex-1为实际索引
  31. };
  32. #endif /* defined(__MythLeague__UpperLayer__) */
  33. //
  34. //
  35. #include "UpperLayer.h"
  36. using namespace cocos2d;
  37. UpperLayer::UpperLayer(){
  38. }
  39. UpperLayer::~UpperLayer(){
  40. }
  41. bool UpperLayer::init(){
  42. bool l_bResult = true;
  43. do {
  44. if(!TouchLogicLayer::init()){
  45. l_bResult = false;
  46. }
  47. } while (0);
  48. return l_bResult;
  49. }
  50. void UpperLayer::onEnter(){
  51. TouchLogicLayer::onEnter();
  52. }
  53. void UpperLayer::onExit(){
  54. TouchLogicLayer::onExit();
  55. }
  56. void UpperLayer::registerWithTouchDispatcher()
  57. {
  58. cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector();
  59. pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority-1, true);
  60. }
  61. void UpperLayer::appendToTouchDispatch(CCTouchDelegate* p_touchableObject){
  62. //断言,p_touchableLayer是CCLayer*类型, (可以是继承)
  63. CCAssert(dynamic_cast<CCTouchDelegate*>(p_touchableObject) != NULL, "p_touchableLayer must be a layer");
  64. m_touchDispatchTable.push_back(p_touchableObject);
  65. }
  66. #pragma mark 触摸相关
  67. //开始
  68. bool UpperLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
  69. //super
  70. TouchLogicLayer::ccTouchBegan(pTouch, pEvent);
  71. m_iIndex = Common_Empty;
  72. CCTouchDelegate* l_touchAble = NULL;
  73. int l_iIndex = 0;
  74. for (; l_iIndex<m_touchDispatchTable.size(); l_iIndex++) {
  75. l_touchAble = m_touchDispatchTable[l_iIndex];
  76. if (l_touchAble && l_touchAble->ccTouchBegan(pTouch, pEvent))
  77. {
  78. m_iIndex = l_iIndex;
  79. break;
  80. }
  81. }
  82. return true;
  83. }
  84. //移动
  85. void UpperLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
  86. //super
  87. TouchLogicLayer::ccTouchMoved(pTouch, pEvent);
  88. if (m_iIndex >= 0) {
  89. CCTouchDelegate* l_touchAble = m_touchDispatchTable[m_iIndex];
  90. l_touchAble->ccTouchMoved(pTouch, pEvent);
  91. }
  92. }
  93. //结束
  94. void UpperLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
  95. //super
  96. TouchLogicLayer::ccTouchEnded(pTouch, pEvent);
  97. if (m_iIndex >= 0) {
  98. CCTouchDelegate* l_touchAbleLayer = m_touchDispatchTable[m_iIndex];
  99. l_touchAbleLayer->ccTouchEnded(pTouch, pEvent);
  100. }
  101. }
  102. //点击home键或其它方式引起的取消
  103. void UpperLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
  104. }
  105. 所有的弹出层,只要继承一下这个就可以,他的优点是事件分发由自己来控制,比较灵活



来自为知笔记(Wiz)


创建一个弹出层