[cocos2d-x]关于3.x的触摸机制
触摸机制的概念
通过对要监听触摸的节点进行注册,然后自定义相应的触摸函数,由事件监听器实现对触摸的监听并且实现相应的响应动作。
触摸的分类
单点触摸
下面是实现单点触摸监听的步骤:
//第一步:先创建一个要进行触摸检测的精灵
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的触摸机制的更多相关文章
- cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)
源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...
- Cocos2D v3.x中关于重叠触摸层优先级的问题
在Cocos2D v2.x版本中可以通过以下方法设置本层的触摸优先级: [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate ...
- cocos2dx 3.0 触摸机制
在cocos2dx 3.0版本号中,废弃了以往2.x版本号的写法,我们先来看一下Layer.h中的一段代码 /* Callback function should not be deprecated, ...
- [cocos2d] 谁摸了我一下----触摸事件处理
1. 设置接受触摸事件,可在init方法里面写上 [self setTouchEnabled: YES]; 旧版为self.isTouchEnabled = YES; xcode会报Deprecati ...
- 初识Android触摸事件传递机制
前言 今天总结的一个知识点是Andorid中View事件传递机制,也是核心知识点,相信很多开发者在面对这个问题时候会觉得困惑,另外,View的另外一个难题滑动冲突,比如在ScrollView中嵌套Li ...
- cocos2d-x 事件分发机制 ——触摸事件监听
cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...
- cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析
(一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...
- Cocos2d-x3.0触摸
cocos2d-x 3.0開始採用C++11,并開始脱离OC风格,在触摸处理上也作出了改变 C++11带来了Lambda表达式(匿名函数),它将简化程序编写,可使代码更清晰易懂 在旧2.x版本号的触摸 ...
- 【Cocos2d-x 3.x】 事件处理机制源码分析
在游戏中,触摸是最基本的,必不可少的.Cocos2d-x 3.x中定义了一系列事件,同时也定义了负责监听这些事件的监听器,另外,cocos定义了事件分发类,用来将事件派发出去以便可以实现相应的事件. ...
- cocos2d-x 事件分发机制 ——加速计事件监听
加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...
随机推荐
- PHP redis有序集合实现分页
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); //设置 redis 字符 ...
- Vue学习之---浏览器本地存储(8/17)
博客园(纯干货):https://www.cnblogs.com/zheng-yuzhu/ 文章目录 1.基础知识 2.代码实例(localStorage.html) 3.测试效果 4.代码实例(se ...
- Doris开发手记4:倍速性能提升,向量化导入的性能调优实践
最近居家中,对自己之前做的一些工作进行总结.正好有Doris社区的小伙伴吐槽向量化的导入性能表现并不是很理想,就借这个机会对之前开发的向量化导入的工作进行了性能调优,取得了不错的优化效果.借用本篇手记 ...
- 齐博x1如何取消某个标签的缓存时间
标签默认会有缓存, 如果你要强制取消缓存时间的话, 可以加上下面的参数 time="-1"如下图所示 标签默认缓存时间是10分钟, 你也可以改成其它时间 比如 time=" ...
- Conda的使用
conda常用的命令 在Anaconda Powershell Prompt 输入: 1.conda -V检验是否安装及当前conda的版本. 2.conda list查看安装了哪些包 3.conda ...
- 7.httprunner-pytest风格用例
用例设计原则 py文件名以test_开头或者_test结尾 函数名以test_开头 类名以Test开头,并且不能有init初始化方法 所有的包pakege必须有_init_.py文件 pychar ...
- NAS数据存储之NFS搭建和使用
NFS是主流异构平台的共享文件系统之一,能够支持在不同类型的系统之间通过网络进行文件共享,允许一个系统在网络上与他人共享目录和文件.NFS传输协议用于服务器和客户机之间的文件访问和共享通信,从而使客户 ...
- java反序列化_link_six
cc_link_six 0x01前言 经过cc链一的学习,然后jdk的版本一更新那两条链子就不能用了,然后这种反序列化的话就很不不止依赖于cc包的引入还有jdk版本,于是就出现了cc_link_six ...
- ansible回调插件介绍(待完成)
简介 ansible回调插件(callback plugins)允许为事件添加一些额外响应.这里的事件包括了执行任务(task)的结果,例如(ok.failed.unreachable.skipped ...
- C#面试题基础
1.什么是GAC,他的作用是什么? 我的这篇文章有详细的介绍 https://www.cnblogs.com/zxtang/p/14313273.html 2.描述一个被protected inter ...