转自: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. nyist 510昂贵的聘礼

    /* 好好的图论题啊,最短路的应用,dijkstra算法 */ #include <iostream> using namespace std; const int INF=100000; ...

  2. Android-xUtils框架介绍(三)

    继续介绍xUtils的最后两个模块:DbUtils和HttpUtils.首先先介绍第一个SQLite数据库操纵的简单ORM框架,只要能理解xUtils为我们提供的api,相信你也能熟练的把DbUtil ...

  3. 代理服务器squid

    http://www.baidu.com/s?wd=squid%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8&f=12&rsp=0& ...

  4. [CF 471C] MUH and House of Cards

    C. MUH and House of Cards   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elep ...

  5. Cobar分布式数据库的应用与实践

    最新文章:看我如何快速学习.Net(高可用数据采集平台).高并发数据采集的架构应用(Redis的应用) 问题点: 随着项目的增长,数据和数据表也成倍的增长,普通的单点数据库已经无法满足日常的增长的需要 ...

  6. 【转】第一个Linux内核驱动程序

    原文网址:http://blog.csdn.net/nexttake/article/details/8181008 刚看 O’REILLY 写的<LINUX 设备驱动程序>时.作者一再强 ...

  7. Android 删除短信

    1.删除短信的函数,一条一条的删除所有短信 /* * Delete all SMS one by one */ public void deleteSMS() { try { ContentResol ...

  8. 《深入Java虚拟机学习笔记》- 第16章 控制流

    <深入Java虚拟机学习笔记>- 第16章 控制流

  9. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu

    sudo apt-get update sudo apt-get install nginxsudo mkdir -p /var/www/example.com/html sudo chown -R ...

  10. VIP网络水军账号

    作为一个技术部的组长,主管公司用户相关的项目.今天一名营销同事找我说他想长生3000个水军账号,我首先就想到了以下几个问题: 1.如何实现 2.产生水军对运营项目的影响,主要问题就是一个真实性问题. ...