Cocos2d-x开发实例:单点触摸事件

下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程。感受一下它的缺点和优点。该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们。

 

Cocos2d-x开发实例:单点触摸事件

下面我们看看HelloWorldScene.cpp具体的实现代码如下:

  1. bool HelloWorld::init()
  2. {
  3. if( !Layer::init() )
  4. {
  5. returnfalse;
  6. }
  7. ......
  8. setTouchEnabled(true);
  9. //设置为单点触摸
  10. setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
  11. returntrue;
  12. }
  13. bool HelloWorld::onTouchBegan(Touch*touch, Event* event)                                                     ①
  14. {
  15. log("onTouchBegan");
  16. //通过tag(标签)获得BoxA精灵
  17. autoboxA = this->getChildByTag(kBoxA_Tag);                                                                           ②
  18. //如果BoxA精灵被点击
  19. if(this->isTap(boxA, touch))                                                                                                            ③
  20. {
  21. log("BoxAsprite Tap");
  22. boxA->runAction(ScaleBy::create(0.06,1.06));                                                                         ④
  23. returntrue;                                                                                                                                     ⑤
  24. }
  25. //通过tag(标签)获得BoxB精灵
  26. autoboxB = this->getChildByTag(kBoxB_Tag);                                                                   ⑥
  27. //如果BoxB精灵被点击
  28. if(this->isTap(boxB, touch))
  29. {
  30. log("BoxBsprite Tap");
  31. boxB->runAction(ScaleBy::create(0.06,1.06));
  32. returntrue;
  33. }                                                                                                                                                 ⑦
  34. returnfalse;
  35. }
  36. void HelloWorld::onTouchMoved(Touch*touch, Event *event)                                                     ⑧
  37. {
  38. log("onTouchMoved");
  39. //通过tag(标签)获得BoxA精灵
  40. autoboxA = this->getChildByTag(kBoxA_Tag);
  41. //如果BoxA精灵被点击
  42. if(this->isTap(boxA, touch))
  43. {
  44. log("BoxAsprite Tap");
  45. //移动当前按钮精灵的坐标位置
  46. boxA->setPosition(boxA->getPosition()+ touch->getDelta());
  47. return;
  48. }
  49. //通过tag(标签)获得BoxB精灵
  50. autoboxB = this->getChildByTag(kBoxB_Tag);
  51. //如果BoxB精灵被点击
  52. if(this->isTap(boxB, touch))
  53. {
  54. log("BoxBsprite Tap");
  55. //移动当前按钮精灵的坐标位置
  56. boxB->setPosition(boxB->getPosition()+ touch->getDelta());
  57. return;
  58. }
  59. }
  60. void HelloWorld::onTouchEnded(Touch*touch, Event *event)                                                      ⑨
  61. {
  62. log("onTouchEnded");
  63. //通过tag(标签)获得BoxA精灵
  64. autoboxA = this->getChildByTag(kBoxA_Tag);
  65. //如果BoxA精灵被点击
  66. if(this->isTap(boxA, touch))
  67. {
  68. log("BoxAsprite Tap");
  69. boxA->runAction(ScaleTo::create(0.06,1.0));
  70. return;
  71. }
  72. //通过tag(标签)获得BoxB精灵
  73. autoboxB = this->getChildByTag(kBoxB_Tag);
  74. //如果BoxB精灵被点击
  75. if(this->isTap(boxB, touch))
  76. {
  77. log("BoxBsprite Tap");
  78. boxB->runAction(ScaleTo::create(0.06,1.0));
  79. return;
  80. }
  81. }
  82. bool HelloWorld::isTap(Node* node,Touch* touch)                                                                    ⑩
  83. {
  84. //获取触摸点相对Node位置坐标
  85. PointlocationInNode = node->convertToNodeSpace(touch->getLocation());                         ⑪
  86. Sizes = node->getContentSize();                                                                                                    ⑫
  87. Rectrect = Rect(0, 0, s.width, s.height);                                                                                         ⑬
  88. //点击范围判断检测
  89. if(rect.containsPoint(locationInNode))                                                                                         ⑭
  90. {
  91. returntrue;
  92. }
  93. returnfalse;
  94. }

上述代码第①、⑧、⑨行分别定义了三个触摸事件函数,函数的参数touch是在层中的触摸点,event是触摸事件,我们不能使用8.1.3一节的auto target = static_cast<Sprite*>(event->getCurrentTarget())语句获得要点击的精灵,事实上event->getCurrentTarget()函数获得的是事件源,这里的事件源是当前的层,而不是精灵对象。那么我们如何判断是否点击了哪个精灵呢?我的办法是每一个精灵逐一判断。所以,我们在第②~⑤行代码判断精灵BoxA是否被点击,在第⑥~⑦行代码判断精灵BoxB是否被点击。

代码第③行用到了isTap函数,我们在第⑩行定义了该函数,它是用来判断触摸点是否在精灵内,这个判断主要是通过Rect的containsPoint函数判断的。函数中第⑪行代码获取触摸点相对精灵对象本地坐标。第⑫行代码是获得精灵对象的尺寸。第⑬行代码是通过精灵对象的尺寸创建Rect变量。第⑭行代码rect.containsPoint(locationInNode)是判断是否触摸点在精灵对象范围。

更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678
上一篇:python_way ,day25 wmi


下一篇:Linux内核分析第三周学习笔记