`
rayln
  • 浏览: 414543 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ccTouchesBegan 中如何让获取的坐标是相对整个屏幕的坐标,而不是在当前view的坐标

 
阅读更多
ccTouchesBegan 中如何让获取的坐标是相对整个屏幕的坐标,而不是在当前view的坐标
         在cocos2d中,在我们在CCLayer中处理 ccTouchesBegan等类似的touch事件的时候,我们一般用下面的代码来获得当前的用户点击位置:
      
UITouch  *touch=[touches anyObject];  
CGPoint  touchLocation= [touch locationInView:[touch view]];  
  
       CGPoint  glLocation=[[CCDirector sharedDirector] convertToGL:touchLocation];  
       glLocation = [self convertToNodeSpace:glLocation];  


       这里一般用到  
       [touch locationInView:[touch view]]  这句话,这句话的意思是,获得touch在当前view的location 的相对坐标,这个时候有个问题,如果我们只想在当前layer处理这个事件,如果用户点击的view不是我们想要处理的view的时候,这个时候会有问题,导致对用户是否touch在我们想要的区域的判断会有问题。比如下面的判断是基于以上的代码的:
     
//判断是否是touch在ruler的范围内  
   if (  
       glLocation.y >= - SelBarSprite.contentSize.height * 0.5f  &&   
       glLocation.y <=   SelBarSprite.contentSize.height * 0.5f &&  
    glLocation.x >= - SelBarSprite.contentSize.width * 0.5f  &&  
       glLocation.x <=   SelBarSprite.contentSize.width * 0.5f)  
{  
    bTouchInsideBlinderRuler=true;  
    touchBeginPoint=glLocation;  
    touchOldPoint=glLocation;  
}  


    今天总算找到了好的解决方法了,就是使用egalView ,也就是获得touch相对于egalview的坐标,这样获取的坐标就是基于winSize的坐标了,判断区域也不会有问题。

    修改后的代码如下:
   
UITouch  *touch=[touches anyObject];  
       CGPoint   touchLocation = [touch locationInView:[[CCDirector sharedDirector] view]];  
     
CGPoint  glLocation=[[CCDirector sharedDirector] convertToGL:touchLocation];  
       glLocation = [self convertToNodeSpace:glLocation];  


   中间的变化在于:将
  CGPoint  touchLocation= [touch locationInView:[touch view]];
   替换成了:
  CGPoint   touchLocation = [touch locationInView:[[CCDirector sharedDirector] view]];
  这要获得的就是基于egalView的坐标了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics