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

 
下面我们看看HelloWorldScene.cpp具体的实现代码如下:
 
- bool HelloWorld::init()  
 
- {  
 
-     if( !Layer::init() )  
 
-     {  
 
-          returnfalse;  
 
-     }  
 
-     ......  
 
-     setTouchEnabled(true);     
 
-     //设置为单点触摸  
 
-     setTouchMode(Touch::DispatchMode::ONE_BY_ONE);                                                               
 
-      
 
-     returntrue;  
 
- }  
 
-    
 
- bool HelloWorld::onTouchBegan(Touch*touch, Event* event)                                                     ①  
 
- {  
 
-     log("onTouchBegan");  
 
-     //通过tag(标签)获得BoxA精灵  
 
-     autoboxA = this->getChildByTag(kBoxA_Tag);                                                                           ②  
 
-     //如果BoxA精灵被点击  
 
-     if(this->isTap(boxA, touch))                                                                                                            ③  
 
-     {  
 
-          log("BoxAsprite Tap");  
 
-          boxA->runAction(ScaleBy::create(0.06,1.06));                                                                         ④  
 
-          returntrue;                                                                                                                                     ⑤  
 
-     }  
 
-     //通过tag(标签)获得BoxB精灵  
 
-     autoboxB = this->getChildByTag(kBoxB_Tag);                                                                   ⑥  
 
-     //如果BoxB精灵被点击  
 
-     if(this->isTap(boxB, touch))  
 
-     {  
 
-          log("BoxBsprite Tap");  
 
-          boxB->runAction(ScaleBy::create(0.06,1.06));  
 
-          returntrue;  
 
-     }                                                                                                                                                 ⑦  
 
-     returnfalse;  
 
- }  
 
-    
 
- void HelloWorld::onTouchMoved(Touch*touch, Event *event)                                                     ⑧  
 
- {  
 
-     log("onTouchMoved");  
 
-     //通过tag(标签)获得BoxA精灵  
 
-     autoboxA = this->getChildByTag(kBoxA_Tag);  
 
-     //如果BoxA精灵被点击  
 
-     if(this->isTap(boxA, touch))  
 
-     {  
 
-          log("BoxAsprite Tap");  
 
-          //移动当前按钮精灵的坐标位置  
 
-          boxA->setPosition(boxA->getPosition()+ touch->getDelta());  
 
-          return;  
 
-     }  
 
-     //通过tag(标签)获得BoxB精灵  
 
-     autoboxB = this->getChildByTag(kBoxB_Tag);  
 
-     //如果BoxB精灵被点击  
 
-     if(this->isTap(boxB, touch))  
 
-     {  
 
-          log("BoxBsprite Tap");  
 
-          //移动当前按钮精灵的坐标位置  
 
-          boxB->setPosition(boxB->getPosition()+ touch->getDelta());  
 
-          return;  
 
-     }  
 
- }  
 
-    
 
- void HelloWorld::onTouchEnded(Touch*touch, Event *event)                                                      ⑨  
 
- {  
 
-     log("onTouchEnded");      
 
-     //通过tag(标签)获得BoxA精灵  
 
-     autoboxA = this->getChildByTag(kBoxA_Tag);  
 
-     //如果BoxA精灵被点击  
 
-     if(this->isTap(boxA, touch))  
 
-     {  
 
-          log("BoxAsprite Tap");  
 
-          boxA->runAction(ScaleTo::create(0.06,1.0));  
 
-          return;  
 
-     }  
 
-     //通过tag(标签)获得BoxB精灵  
 
-     autoboxB = this->getChildByTag(kBoxB_Tag);  
 
-     //如果BoxB精灵被点击  
 
-     if(this->isTap(boxB, touch))  
 
-     {  
 
-          log("BoxBsprite Tap");  
 
-          boxB->runAction(ScaleTo::create(0.06,1.0));  
 
-          return;  
 
-     }  
 
- }  
 
-    
 
- bool HelloWorld::isTap(Node* node,Touch* touch)                                                                    ⑩  
 
- {  
 
-     //获取触摸点相对Node位置坐标  
 
-     PointlocationInNode = node->convertToNodeSpace(touch->getLocation());                         ?  
 
-     Sizes = node->getContentSize();                                                                                                    ?  
 
-     Rectrect = Rect(0, 0, s.width, s.height);                                                                                         ?  
 
-    
 
-     //点击范围判断检测  
 
-     if(rect.containsPoint(locationInNode))                                                                                         ?  
 
-     {  
 
-          returntrue;  
 
-     }  
 
-     returnfalse;  
 
- }  
 
 
 
 
上述代码第①、⑧、⑨行分别定义了三个触摸事件函数,函数的参数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技术讨论群:257760386、327403678
 
 
Cocos2d-x开发实例:单点触摸事件
原文:http://www.cnblogs.com/iOS-Blog/p/3870533.html