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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值