转自: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. tlplayer 所有平台版本支持水印叠加

    tlplayer支持视频渲染前水印叠加,各个系统版本同样支持. 联系方式:weinyzhou86@gmail.com QQ:514540005 版权所有,禁止转载. 发布自:http://blog.c ...

  2. Android开发之ListView-SimpleAdapter的使用

    SimpleAdapter: SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int r ...

  3. Android开发之隐式Intent中Intent-filter的三个属性-action,category,data

    使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...

  4. 一种基于Storm的可扩展即时数据处理架构思考

    问题引入 使用storm可以方便的构建一种集群式的数据框架,并通过定义topo来实现业务逻辑. 但使用topo存在一个缺点, topo的处理能力来自于其启动时设置的worker数目,在很多情况下,我们 ...

  5. android ListView注意事项

    所有问题,都是自己遇到过的. 但内容,有一半是自己写的,也有一半是复制过来. 所以,写成原创还请原谅 1. ListView添加标题后(addHeader())后,使用listView.getAdap ...

  6. [转]javascript console 函数详解 js开发调试的利器

    javascript console 函数详解 js开发调试的利器   分步阅读 Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该 ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.8

    Prove that for any matrices $A,B$ we have $$\bex |\per (AB)|^2\leq \per (AA^*)\cdot \per (B^*B). \ee ...

  8. HDU 5701 中位数计数 暴力

    老题了,附上黄学长链接一发,直接改改就AC了,http://hzwer.com/1216.html #include <cstdio> #include <iostream> ...

  9. ZOJ 3469 Food Delivery 区间DP

    这道题我不会,看了网上的题解才会的,涨了姿势,现阶段还是感觉区间DP比较难,主要是太弱...QAQ 思路中其实有贪心的意思,n个住户加一个商店,分布在一维直线上,应该是从商店开始,先向两边距离近的送, ...

  10. Robotium 系列(2) - 简单介绍Monkey和MonkeyRunner

    除了Robotium,Android还有其他的自动化测试方法,比如Monkey和MonkeyRunner. 这里就做一个简单的介绍和使用方法. 本文提纲: 1. Android SDK以及SDK中的工 ...