触摸机制的概念

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

触摸的分类

单点触摸

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

//第一步:先创建一个要进行触摸检测的精灵
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. 分支结构中的if-else(条件判断结构)

    一.三种结构 第一种: if(条件表达式){ 执行表达式}第二种:二选一 if(条件表达式){ 执行表达式1}else{ 执行表达式2}第三种:n选一 if(条件表达式){ 执行表达式1}else i ...

  2. 2022年最新最详细的tomcat安装教程和常见问的解决

    文章目录 1.官网直接下载 1.1.jdk的版本和tomcat版本应该相对应或者兼容 1.2. 在官网找对应的tomcat版本进行下载 1.3 .根据电脑版本下载64-bit windows zip( ...

  3. Java模拟生产者-消费者问题。生产者不断的往仓库中存放产品,消费者从仓库中消费产品。其中生产者和消费者都可以有若干个。在这里,生产者是一个线程,消费者是一个线程。仓库容量有限,只有库满时生产者不能存

    需求分析:生产者生产产品,存放在仓库里,消费者从仓库里消费产品. 程序分析: 1.生产者仅仅在仓储未满时候生产,仓满则停止生产. 2.消费者仅仅在仓储有产品时候才能消费,仓空则等待. 3.当消费者发现 ...

  4. 齐博x1嵌套-循环栏目,并列出子栏目下的内容

    嵌套,循环栏目,并列出子栏目下的内容. 代码如下: <div class="channel-list"> <div class="row"&g ...

  5. 累加和为 K 的子数组问题

    累加和为 K 的子数组问题 作者:Grey 原文地址: 博客园:累加和为 K 的子数组问题 CSDN:累加和为 K 的子数组问题 题目说明 数组全为正数,且每个数各不相同,求累加和为K的子数组组合有哪 ...

  6. 字符串匹配(BF算法和KMP算法及改进KMP算法)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include<cstring> ...

  7. 各种优化器对比--BGD/SGD/MBGD/MSGD/NAG/Adagrad/Adam

    指数加权平均 (exponentially weighted averges) 先说一下指数加权平均, 公式如下: \[v_{t}=\beta v_{t-1}+(1-\beta) \theta_{t} ...

  8. yum 更新yum源

    yum 更新yum源 # 1.做好备份,防止更新失败时切换回去 $ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base. ...

  9. web安全学习(sql注入1)

    web安全学习(sql注入1) 一.简介 sql语句就是数据库语句,而sql注入就是用户将自己构造的恶意sql语句提交,然后服务器执行提交的危险语句.sql注入可能造成信息泄露以及服务器被控制等危害. ...

  10. ui自动化测试数据复原遇到的坑——2、python连接informix时pytest报致命错误Windows fatal exception: access violation

    python连接informix只能通过jdbc(需要先部署java环境.我试过到IBM上下载ODBC但结局是失败的),在执行pytest时发现有一串报错(大致是下面的这样): Windows fat ...