在做练习,触摸故障,看到源代码,以了解下触摸事件.

练习操作:直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处理方法,例如以下:

  1. -(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]就可以实现。

  1. -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  2. -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  3. -(void) ccTouchesCancelled:(NSSet*)touch withEvent:(UIEvent *)event;

这三个方法和ccTouchesBegan类似。

Targeted Touch Delegate方式

在standard方式中的响应处理事件处理的都是NSSet,而 targeted方式仅仅处理单个的UITouch对象,在多点触摸条件下,应该採纳standard方式。在使用targeted方式之前须要重写CCLayer中的registerWithTouchDispatcher方法:

  1. //记得在头文件里导入“CCTouchDispatcher.h”
  2. -(void) registerWithTouchDispatcher {
  3. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  4. }

targeted方式中用户须要重载4个主要的处理方法。当中ccTouchBegan必须重写,其它三个是可选的。

  1. - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; (必须实现)
  2. - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
  3. - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
  4. - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;

每次touch事件发生时,先调用ccTouchBegan方法,该方法对每一个UITouch进行响应并返回一个BOOL值。若为YES,则兴许的ccTouchMoved、ccTouchEnabled和ccTouchCancelled才会接着响应。

多点触摸支持

在xxxAppDelegate类的applicationDidFinishLaunching方法中增加以下代码

  1. [glView setMultipleTouchEnabled:YES];
 
 
 
关于swallowsTouches

[[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)的更多相关文章

  1. 深入cocos2d-x中的touch事件

    深入cocos2d-x中的touch事件 在文章cocos2d-x中处理touch事件中简单讨论过怎样处理touch事件, 那么今天来深入了解下cocos2d-x中是怎样分发touch事件的. 我们最 ...

  2. Android touch事件的派发流程

    Android TouchEvent事件传递机制 通俗易懂,能够了解Touch事件派发的基本流程. Android中的dispatchTouchEvent().onInterceptTouchEven ...

  3. 关于touch事件对于性能的影响

    第一次写博客随笔,废话不多说,直接进入正题. 最近一直专注于移动终端的开发,碰到了一个比较棘手的事情,就是touch事件,大家都知道,touch事件有几种,无非就是touchstart,touchmo ...

  4. 移动端touch事件 || 上拉加载更多

    前言: 说多了都是泪,在进行项目开发时,在上拉加载更多实现分页效果的问题上,由于当时开发任务紧急,所以就百度找了各种移动端的上拉下拉 实现加载更多的插件.然后就留下了个坑:上拉加载的时候会由于用户错误 ...

  5. Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇

    2015-11-26 17:00:22 前言:Android的Touch事件传递和View的实现紧密相连,因此理解Touch事件的传递,有助于我们更好的理解View的工作原理. 1. 几个重要的方法: ...

  6. Touch事件机制

    1.概念 Touch事件分发中有三个主角:Activity.ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewG ...

  7. React-Native系列Android——Touch事件原理及状态效果

    Native原生相比于Hybrid或H5最大长处是具有流畅和复杂的交互效果,触摸事件便是当中重要一项,包括点击(Click).长按(LongClick).手势(gesture)等. 以最简单常见的点击 ...

  8. 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑

    前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...

  9. 自定义View系列教程07--详解ViewGroup分发Touch事件

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

随机推荐

  1. HTML5初步——新的表单元素和属性

    HTML5初步--新的表单元素和属性 HTML5初步--新的表单元素和属性 <!DOCTYPE html> <html> <head> <meta chars ...

  2. 浅谈OCR之Onenote 2010

    原文:浅谈OCR之Onenote 2010 上一次我们讨论了Tesseract OCR引擎的用法,作为一款老牌的OCR引擎,目前已经开源,最新版本3.0中更是加入了中文OCR功能,再加上Google的 ...

  3. RAC优化大框架的分配(jumbo frame)

    RAC优化大框架的分配(jumbo frame) 首先讲讲MTU的概念:在网络通信中,有个MTU(Max Transmission Unit)的概念,即网络传输中最大帧的大小,这个值默认是1500By ...

  4. Codeforces 452A Eevee

    #include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...

  5. Get与Post的差别

    Http定义了与server交互的不同方法,最主要的方法有4种,各自是GET,POST.PUT,DELETE. URL全称是资源描写叙述符.我们能够这样觉得:一个URL地址,它用于描写叙述一个网络上的 ...

  6. Eclipse常用热键

    ,Ctrl+D 删除选中的几行 ,Alt+上下箭头 移动选中的代码块 ,Alt+左右箭头 回退 前进 ,Alt+Shift+上下箭头 复制选中的代码块 ,sysout+Ctrl space 生成Sys ...

  7. bootstrap之DumpWindowHierarchy

    DumpWindowHierarchy package io.appium.android.bootstrap.handler; import android.os.Environment; impo ...

  8. SQL Server Insert操作中的锁

    原文:SQL Server Insert操作中的锁 这篇博文简单介绍一下在SQL Server中一条Insert语句中用到的锁. 准备数据 首先我们建立一张表Table_1,它有两列Id(bigint ...

  9. python : 批量下载R语言库包

    soupR.py 代码例如以下 # -*- coding: cp936 -*- import urllib import urllib2 import os, re from BeautifulSou ...

  10. iOS 获取高速随机路径sandbox目录

    NSLog(@"%@", NSHomeDirectory());//沙盒主目录 NSLog(@"%@", NSTemporaryDirectory());//砂 ...