CCLayer在Touch事件(Standard Touch Delegate和Targeted Touch Delegate)
在做练习,触摸故障,看到源代码,以了解下触摸事件.
练习操作:直CClayer子类init在 this->setTouchEnabled(true);
事件处理方法覆盖
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
结果按键没反应
由于setTouchEnabled(true); 开启多点触摸, 而事件处理方法是针对单点的,所以不行.
解决方法1.
覆盖事件多点处理方法
// default implements are used to call script callback if exist
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
从參数类型CCSet能够看出此參数是集合,应该是多个按的点.
解决方法2.
覆盖onEnter(),加上单点事件托付
onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCLayer::onEnter();//这个要要加
}
CClayer::onEnter()
{
....
if (m_bTouchEnabled) //这个m_bTouchEnabled就是setTouchEnabled(true)设置的
{
this->registerWithTouchDispatcher();//会设置Standard Touch Delegate,这也是为什么CCLayer默认採纳这样的方式
}
.....
}
touch 事件分发顺序
cocos2d-x 首先派发事件给CCTargetedTouchDelegate。 再派发事件给CCStandardTouchDelegate。对于同样类型的TouchDelegate, 则是依据注冊的优先级
来确定派发先后顺序。假设优先级也一样,则依照注冊的顺序派发事件。
-------------------------------------------------------------------------------------------------------------------------
以下是别人总结分享的 http://www.cnblogs.com/pengyingh/articles/2435160.html
Cocos2d 开发中提供了两种touch处理方式,Standard Touch Delegate和 Targeted Touch Delegate方式(參见CCTouchDelegateProtocol.h中源码),CCLayer默认是採用第一种方式(參见CCLayer的 registerWithTouchDispatcher方法)。
在CCLayer子类中要能接收touch事件。首先须要激活touch支持。在init方法中设置isTouchEnabled值为YES。
Standard Touch Delegate(CCLayer默认採纳这样的方式)
Standard方法中用户须要重载四个主要的touch处理方法,例如以下:
- -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
当touch事件发生时。会调用该方法响应touch事件。假设是单点touch,则仅仅须要调用 UITouch *touch = [touches anyObject],就能够获取touch对象。假设须要响应多点 touch。则须要调用[[event allTouches] allObjects]返回一个UITouch的NSArray对象。然后使用NSArray的objectAtIndex依次訪问各个UITouch对象。
为了获取UITouch对象的坐标(如果该UITouch名称为touch),调用[touch locationInView: [ touch view]]会返回一个UIView相关的坐标viewPoint。
使用Cocos2d的新建应用程序向导创建一个新的cocos2d application时,在xxxAppDelegate类的applicationDidFinishLaunching方法中CCDirector会将UIView转换为支持OpenGL ES的EAGLView。
此时。我们还须要将前面获取的UIView中的viewPoint转换为EAGLView坐标,调用[[CCDirector sharedDirector] convertToGL: viewPoint]就可以实现。
- -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- -(void) ccTouchesCancelled:(NSSet*)touch withEvent:(UIEvent *)event;
这三个方法和ccTouchesBegan类似。
Targeted Touch Delegate方式
在standard方式中的响应处理事件处理的都是NSSet,而 targeted方式仅仅处理单个的UITouch对象,在多点触摸条件下,应该採纳standard方式。在使用targeted方式之前须要重写CCLayer中的registerWithTouchDispatcher方法:
- //记得在头文件里导入“CCTouchDispatcher.h”
- -(void) registerWithTouchDispatcher {
- [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
- }
targeted方式中用户须要重载4个主要的处理方法。当中ccTouchBegan必须重写,其它三个是可选的。
- - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; (必须实现)
- - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
每次touch事件发生时,先调用ccTouchBegan方法,该方法对每一个UITouch进行响应并返回一个BOOL值。若为YES,则兴许的ccTouchMoved、ccTouchEnabled和ccTouchCancelled才会接着响应。
多点触摸支持
在xxxAppDelegate类的applicationDidFinishLaunching方法中增加以下代码
- [glView setMultipleTouchEnabled:YES];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];
假设 swallowsTouches:YES && touch begin return yes
那么他的move 和end就接受。,别的类就不再接受了。
假设swallowsTouches:NO &&begin return yes
那么他的move 和end接受。其他类仍然能够接受。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
CCLayer在Touch事件(Standard Touch Delegate和Targeted Touch Delegate)的更多相关文章
- 深入cocos2d-x中的touch事件
深入cocos2d-x中的touch事件 在文章cocos2d-x中处理touch事件中简单讨论过怎样处理touch事件, 那么今天来深入了解下cocos2d-x中是怎样分发touch事件的. 我们最 ...
- Android touch事件的派发流程
Android TouchEvent事件传递机制 通俗易懂,能够了解Touch事件派发的基本流程. Android中的dispatchTouchEvent().onInterceptTouchEven ...
- 关于touch事件对于性能的影响
第一次写博客随笔,废话不多说,直接进入正题. 最近一直专注于移动终端的开发,碰到了一个比较棘手的事情,就是touch事件,大家都知道,touch事件有几种,无非就是touchstart,touchmo ...
- 移动端touch事件 || 上拉加载更多
前言: 说多了都是泪,在进行项目开发时,在上拉加载更多实现分页效果的问题上,由于当时开发任务紧急,所以就百度找了各种移动端的上拉下拉 实现加载更多的插件.然后就留下了个坑:上拉加载的时候会由于用户错误 ...
- Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇
2015-11-26 17:00:22 前言:Android的Touch事件传递和View的实现紧密相连,因此理解Touch事件的传递,有助于我们更好的理解View的工作原理. 1. 几个重要的方法: ...
- Touch事件机制
1.概念 Touch事件分发中有三个主角:Activity.ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewG ...
- React-Native系列Android——Touch事件原理及状态效果
Native原生相比于Hybrid或H5最大长处是具有流畅和复杂的交互效果,触摸事件便是当中重要一项,包括点击(Click).长按(LongClick).手势(gesture)等. 以最简单常见的点击 ...
- 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...
- 自定义View系列教程07--详解ViewGroup分发Touch事件
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
随机推荐
- 超人学院Hadoop大数据技术资源分享
超人学院Hadoop大数据技术资源分享 http://bbs.superwu.cn/forum.php?mod=viewthread&tid=807&fromuid=645 很多其它精 ...
- Doxgen+Graphiz+htmlhelp配置
查看一些开源码常常被一些函数的调用关系给绕进去.找个工具生成个调用关系图或简单的文档对于帮助阅读程序有非常大的帮助. 1 doxgen+graphviz+htmlhelp简单介绍 1.1 doxgen ...
- Android比较字符串是空的(isEmpty)
通常情况下,我们需要去推断一个字符串变量是否为空,今天,我特意做了一个小测试 StringUtils.java: package com.yx.equipment_collection.utils; ...
- Selenium 验证picklist是可被正确选中且是有序的(动态数组赋值)
原代码: <select id="edit-submitted-im-interesting-in" class="form-select required&quo ...
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...
- HTML表格标签的使用-<table>
<html> <head> <title> 表格标签 </title> <!-- 标签名:table 定义一个表格 子标签:<caption ...
- mysql增量ID
启动值更改方法
在mysql很多朋友感到场AUTO_INCREMENT增量型ID值它不能被改变,其实这种认识是错误的,这里mysql增量ID开始值更改和设置. 设置自动递增字段的通常的方法: 格时加入: create ...
- 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...
- Linux 远程查看tomcat控制台
我现在只说如何看远程的tomcat控制台命令. 用远程登陆客户端登陆linux进入tomcat/logs/文件夹下键入指令:tail -f catalina.out ctrl + c 退出 这样就可 ...
- notepad扩展搜索,正则搜索
Dos和windows采用回车+换行CR/LF表示下一行, 0d 0a 两个字节表示而UNIX/Linux采用’\n’换行符LF表示下一行(ASCII代码是10),0a一个字节表示MAC OS系统则采 ...