转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019893.html

//重载
virtual bool ccTouchBegan(CCTouch *touch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *touch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *touch, CCEvent *pEvent);
virtual void onEnter();
virtual void onExit(); //添加支持触摸事件
void CTestLayer::onEnter()
{
  CCLayer::onEnter();
  this->setTouchEnabled(true);
  CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, , true);
} void CTestLayer::onExit()
{
  CCLayer::onExit();
  CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//用自己的坐标系相对于原点进行判断
bool checkTouchInSelf(CCTouch *touch);
//用自己的坐标系相对于锚点进行判断
bool checkTouchInSelf_AR(CCTouch *touch);
//用父元素坐标系及自己在父坐标中的位置进行判断
bool checkTouchInSelf_Parent(CCTouch *touch); //______________________________________________________________________________________________
//用自己的坐标系相对于原点进行判断
bool CTestLayer::checkTouchInSelf(CCTouch *touch)
{
  //方案一
  //将点击点转换成自己坐标系中的坐标,相对于0,0点
  CCPoint pt = convertTouchToNodeSpace(touch);
  printf("pt.x=%.1f pt.y=%.1fn", pt.x, pt.y);
  int nw = getContentSize().width;
  int nh = getContentSize().height;
  CCRect rc(, , nw, nh);
  if(rc.containsPoint(pt))
  {
    //获得点击的OpenGL的世界坐标值
    CCPoint touchPoint = touch->getLocation();
    printf("ccTouchBegan x=%.1f y=%.1fn", touchPoint.x, touchPoint.y);
    return true;
  }
  return false;
}
//______________________________________________________________________________________________
//用自己的坐标系相对于锚点进行判断
bool CTestLayer::checkTouchInSelf_AR(CCTouch *touch)
{
  //方案二
  //将点击点转换成自己坐标系中的坐标,相对于锚点
  CCPoint ptAR = convertTouchToNodeSpaceAR(touch);
  printf("ptAR.x=%.1f ptAR.y=%.1fn", ptAR.x, ptAR.y);
  CCPoint pp = this->getAnchorPoint();
  int nw = getContentSize().width;
  int nh = getContentSize().height;
  int nx = -(nw * pp.x);
  int ny = -(nh * pp.y);
  CCRect rcar(nx, ny, nw, nh);
  if(rcar.containsPoint(ptAR))
  {
    //获得点击的OpenGL的世界坐标值
    CCPoint touchPoint = touch->getLocation();
    printf("ccTouchBegan x=%.1f y=%.1fn", touchPoint.x, touchPoint.y);
    return true;
  }
  return false;
}
//______________________________________________________________________________________________
//用父元素坐标系及自己在父坐标中的位置进行判断
bool CTestLayer::checkTouchInSelf_Parent(CCTouch *touch)
{
  //方案三
  //获得点击的OpenGL的世界坐标值
  CCPoint touchPoint = touch->getLocation();
  //将点击的位置转换成父元素坐标系中的相对坐标
  CCPoint pt=getParent()->convertToNodeSpace(touchPoint);
  printf("pt.x=%.1f, pt.y=%.1fn", pt.x, pt.y);
  //得到自己在父元素坐标系中的位置范围
  CCRect rect=boundingBox();
  printf("rect.l=%.1f, rect.b=%.1f, rect.r=%.1f, rect.t=%.1fn",
  rect.getMinX(), rect.getMinY(), rect.getMaxX(), rect.getMaxY());
  //判断是否点击落在自己的范围当中, 以上判断全是在父元素坐标系中进行计算
  if(rect.containsPoint(pt))
  {
    printf("ccTouchBegan x=%.1f y=%.1fn", touchPoint.x, touchPoint.y);
    return true;
  }
  return false;
}

cocos2d-x 判断点击命中坐标的几种方法的更多相关文章

  1. 【九天教您南方cass 9.1】 09 提取坐标的几种方法

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...

  2. Android监听点击事件实现的三种方法

    监听点击事件实现的三种方法:1.匿名内部类2.外部类3.直接实现接口 1.匿名内部类: package com.jereh.calculator; import android.content.Con ...

  3. 使用c#检测文件正在被那个进程占用 判断文件是否被占用的两种方法

    C# 判断文件是否被占用的三种方法 using System.IO; using System.Runtime.InteropServices; [DllImport("kernel32.d ...

  4. Java 判断字符串是否为空的四种方法、优缺点与注意事项

    以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: ...

  5. VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法

    原文:VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/ ...

  6. Java:判断字符串是否为数字的五种方法

    Java:判断字符串是否为数字的五种方法 //方法一:用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str. ...

  7. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  8. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. JAVA中判断char是否是中文的几种方法

    1.方法一 char c = 'a'; if((c >= 0x4e00)&&(c <= 0x9fbb)) { System.out.println("是中文&qu ...

随机推荐

  1. How to remove spaces of a string with regular expression

    left 's/^\s*//g' right 's/\s*$//g' all 's/\s+//g'

  2. SpringMVC中对Controller使用AOP

    转自http://usherlight.iteye.com/blog/1306111 正确配置spring aop,在controller中使用AOP 在controller中使用AOP的问题主要在于 ...

  3. 从Hadoop框架与MapReduce模式中谈海量数据处理(含淘宝技术架构) (转)

    转自:http://blog.csdn.net/v_july_v/article/details/6704077 从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到 ...

  4. js不能执行,IE处理方法

    一.如果你的ie不能打开js脚本(连系统里所有的js文件都不运行,网页上的js广告或好多页面都显示不了),请按一下步骤进行排查与解决: 1.查看是否IE的安全里面禁止了JS的运行: 将工具=>i ...

  5. I.MX6 Android 移除 Settings wifi功能

    /********************************************************************* * I.MX6 Android 移除 Settings w ...

  6. WPF 中资源路径的问题

    WPF 中资源路径的问题 1. 引用当前工程的资源(注意xxxx.png的build action 应设置为Resource 或Embedded Resource) <ImageBrush Im ...

  7. HDU4578 Transformation 线段树

    这个题让我重新学习了加 乘 在区间的操作 题解:http://blog.csdn.net/guognib/article/details/25324025?utm_source=tuicool& ...

  8. 2014 多校联合训练赛6 Fighting the Landlords

    本场比赛的三个水题之一,题意是两个玩家每人都持有一手牌,问第一个玩家是否有一种出牌方法使得在第一回和对方无牌可出.直接模拟即可,注意一次出完的情况,一开始没主意,wa了一发. #include< ...

  9. [selenium webdriver Java]隐式的等待同步

    Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...

  10. PPTP VPN不能打开百度

    问题: 在阿里云上设置PPTP VPN,电脑能正常连接,能打开京东 淘宝 QQ等没有问题,但是不能打开百度  糯米等网站.开始怀疑是代理设置问题,后面确认未设置独立规则. 1.从应用层看排除特殊规则设 ...