触摸机制的概念

通过对要监听触摸的节点进行注册,然后自定义相应的触摸函数,由事件监听器实现对触摸的监听并且实现相应的响应动作。

触摸的分类

单点触摸

下面是实现单点触摸监听的步骤:

//第一步:先创建一个要进行触摸检测的精灵
auto spr=Sprite::create("helloworld.png");
//第二步:创建一个事件监听器
auto touchListener= EventListenerTouchOneByOne::create();
//第三步:设置不让触摸检测传递到下一层
touchListener->setSwallowTouches(true); //******重点*****
//第四步(第一种方法):设置相应的触摸函数
//先在头文件中声明四个回调函数:
std::function<bool(Touch*t, Event*)>onTouchBegan;
std::function<void(Touch*, Event*)>onTouchMoved;
std::function<void(Touch*, Event*)>onTouchEnded;
std::function<void(Touch*, Event*)> onTouchCancelled;
//然后在设置对应函数:
listner>onTouchBegan=CC_CALLBACK_2(GameTest::onTouchBegan,this);
listner->onTouchMoved=CC_CALLBACK_2(GameTest::onTouchMoved, this);
listner->onTouchEnded=CC_CALLBACK_2(GameTest::onTouchEnded, this);
//将监听器添加带事件派发器中
_eventDispatcher->addEventListenerWithSceneGraphPriority(listner, spr);
//给监听器设置优先级,越高越先响应
listner->setFixedPriority(1); //第四步(第二种方法):使用lambda表达式,这种更加方便
listner->onTouchBegan = [](Touch*t, Event*e) { return true; };
listner->onTouchMoved = [](Touch*t, Event*e) {};
listner->onTouchEnded = [](Touch*t, Event*e) {};

多点触摸

多点触摸和单点触摸类似,不过使用的参数使用vector数组来存储了多个touch触摸点的坐标位置,下面是不同的地方

std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesBegan;  

std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesMoved;  

std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesEnded;  

std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesCancelled;

触摸函数的两个参数的含义

Touch*的作用:传入触摸点的坐标位置,下面是Touch类中提供的一些方法。

    Vec2 getLocation() const;
/** Returns the previous touch location in OpenGL coordinates.
*
* @return The previous touch location in OpenGL coordinates.
*/
Vec2 getPreviousLocation() const;
/** Returns the start touch location in OpenGL coordinates.
*
* @return The start touch location in OpenGL coordinates.
*/
Vec2 getStartLocation() const;
/** Returns the delta of 2 current touches locations in screen coordinates.
*
* @return The delta of 2 current touches locations in screen coordinates.
*/
Vec2 getDelta() const;
/** Returns the current touch location in screen coordinates.
*
* @return The current touch location in screen coordinates.
*/
Vec2 getLocationInView() const;
/** Returns the previous touch location in screen coordinates.
*
* @return The previous touch location in screen coordinates.
*/
Vec2 getPreviousLocationInView() const;
/** Returns the start touch location in screen coordinates.
*
* @return The start touch location in screen coordinates.
*/
Vec2 getStartLocationInView() const; /** Set the touch information. It always used to monitor touch event.
*
* @param id A given id
* @param x A given x coordinate.
* @param y A given y coordinate.
*/

Event*作用:主要是传入要进行操作的对象比如:键盘,鼠标,游戏手柄等等,下面是它提供的一些函数。

    Type getType() const { return _type; }

    /** Stops propagation for current event.
*/
void stopPropagation() { _isStopped = true; } /** Checks whether the event has been stopped.
*
* @return True if the event has been stopped.
*/
bool isStopped() const { return _isStopped; } /** Gets current target of the event.
* @return The target with which the event associates.
* @note It's only available when the event listener is associated with node.
* It returns 0 when the listener is associated with fixed priority.
*/
Node* getCurrentTarget() { return _currentTarget; }

其他注意事项

virtual bool onTouchBegan( Touch *Touch, Event *Event);

①如果返回false,则本层的onTouchMoved(),onTouchEnded()不会再接收到消息,但是本层之下的其它层会接收到消息

②如果返回true,则本层的onTouchMoved(),onTouchEnded()可以接收到消息,但是本层之下的其它层不能再接收到消息

[cocos2d-x]关于3.x的触摸机制的更多相关文章

  1. cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)

    源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...

  2. Cocos2D v3.x中关于重叠触摸层优先级的问题

    在Cocos2D v2.x版本中可以通过以下方法设置本层的触摸优先级: [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate ...

  3. cocos2dx 3.0 触摸机制

    在cocos2dx 3.0版本号中,废弃了以往2.x版本号的写法,我们先来看一下Layer.h中的一段代码 /* Callback function should not be deprecated, ...

  4. [cocos2d] 谁摸了我一下----触摸事件处理

    1. 设置接受触摸事件,可在init方法里面写上 [self setTouchEnabled: YES]; 旧版为self.isTouchEnabled = YES; xcode会报Deprecati ...

  5. 初识Android触摸事件传递机制

    前言 今天总结的一个知识点是Andorid中View事件传递机制,也是核心知识点,相信很多开发者在面对这个问题时候会觉得困惑,另外,View的另外一个难题滑动冲突,比如在ScrollView中嵌套Li ...

  6. cocos2d-x 事件分发机制 ——触摸事件监听

    cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...

  7. cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析

    (一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...

  8. Cocos2d-x3.0触摸

    cocos2d-x 3.0開始採用C++11,并開始脱离OC风格,在触摸处理上也作出了改变 C++11带来了Lambda表达式(匿名函数),它将简化程序编写,可使代码更清晰易懂 在旧2.x版本号的触摸 ...

  9. 【Cocos2d-x 3.x】 事件处理机制源码分析

    在游戏中,触摸是最基本的,必不可少的.Cocos2d-x 3.x中定义了一系列事件,同时也定义了负责监听这些事件的监听器,另外,cocos定义了事件分发类,用来将事件派发出去以便可以实现相应的事件. ...

  10. cocos2d-x 事件分发机制 ——加速计事件监听

    加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...

随机推荐

  1. java中GC的日志认识详解

    不同的垃圾回收器 他们的日志都是完成不一样的,看懂日志是解决和发现问题的重中之重. Parallel Scavenge + Parallel Old 日志 启动参数 -XX:+UseParallelG ...

  2. JAVA学习前准备

    电脑常用快捷键 Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+A:全选 Ctrl+X:剪切 Ctrl+Z:撤销 Ctrl+S:保存 Alt+F4:关闭窗口 Shift+delete:永久性删除文件 ...

  3. 使用rsync向服务器迁移大文件

    场景 本人将12G本地单文件(12G大小h5文件数据集)向Linux服务器进行大文件上传时传输失败.最初使用 scp 命令或 rsync 直接对大文件进行传输,会出现网络断开或服务器端管道破裂情况,而 ...

  4. 修改linux系统时间

    在Linux系统中,可以用date命令来显示或设定系统的日期与时间 1. 查看系统时间 [root@iZ2ze0gm3scdypc0i15r8yZ ~]# date Tue Aug 16 00:10: ...

  5. 【操作说明】全能型H.265播放器如何使用?

    本播放器集成了公司业务的接口,包含了实播,回放,云台控制和回放速度控制,截图和全屏功能可以根据type直接初始化接口地址如果是第三方业务对接,也可以单独配置接口地址 正确使用H.265播放器需要按以下 ...

  6. 用于数据科学的顶级 C/C++ 机器学习库整理

    用于数据科学的顶级 C/C++ 机器学习库整理 介绍和动机--为什么选择 C++ C++ 非常适合 动态负载平衡. 自适应缓存以及开发大型大数据框架 和库.Google 的MapReduce.Mong ...

  7. java反序列化_link_six

    cc_link_six 0x01前言 经过cc链一的学习,然后jdk的版本一更新那两条链子就不能用了,然后这种反序列化的话就很不不止依赖于cc包的引入还有jdk版本,于是就出现了cc_link_six ...

  8. day17-Servlet06

    Servlet06 15.HttpServletResponse 15.1HttpServletResponse介绍 每次HTTP请求,Tomcat都会创建一个HttpServletResponse对 ...

  9. 方法的重载(Overload)+ println重载

    方法的重载(Overload) package cn.day01; /*方法的重载(Overload):多个方法的名称一样,但是参数列表不一样. * 好处:只需要记住唯一一个方法名称,就可以实现类似多 ...

  10. FIXMAP内存管理器

    fixed map是被linux kernel用来解决一类问题的机制,这类问题的共同特点是: (1)在很早期的阶段需要进行地址映射,而此时,由于内存管理模块还没有完成初始化,不能动态分配内存,也就是无 ...