首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

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

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

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的坐标了。

热点排行