详解Cocos2d 开发关于CCLayer中Touch事件 以及优先级
来源:互联网 发布:超级兔子是什么软件 编辑:程序博客网 时间:2024/05/10 23:28
/** CCTouchDispatcher.
Singleton that handles all the touch events.
The dispatcher dispatches events to the registered TouchHandlers.
There are 2 different type of touch handlers:
- Standard Touch Handlers
- Targeted Touch Handlers
The Standard Touch Handlers work like the CocoaTouch touch handler: a set of touches is passed to the delegate.
On the other hand, the Targeted Touch Handlers only receive 1 touch at the time, and they can "swallow" touches (avoid the propagation of the event).
Firstly, the dispatcher sends the received touches to the targeted touches.
These touches can be swallowed by the Targeted Touch Handlers. If there are still remaining touches, then the remaining touches will be sent
to the Standard Touch Handlers.
@since v0.8.0
*/
Changes the priority of a previously added delegate. The lower the number,
the higher the priority
target如果声明时主张要拥有,并且在began时返回true,则此事件不会传递下去。
详解Cocos2d 开发关于CCLayer中Touch事件
Cocos2d 开发关于CCLayer中Touch事件是本文要介绍的内容,主要是来学习Cocos2d 开发中Touch事件。Cocos2d作为一个开源的2D游戏引擎,最初是用python语言实现,mac app开发流行后,提供了一个Objective-C的版本。
AD:
Cocos2d 开发关于CCLayer中Touch事件是本文要介绍的内容,主要是来学习Cocos2d 开发中Touch事件。Cocos2d作为一个开源的2D游戏引擎,最初是用python语言实现,mac app开发流行后,提供了一个Objective-C的版本。采用Cocos2d框架开发iphone游戏,极大提高了开发的速度。简单介绍参见百度百科 ,cocos2d官网 。
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];
小结:详解Cocos2d 开发关于CCLayer中Touch事件的内容介绍完了,希望通过本文的学习能对你有所帮助。
【编辑推荐】
- IOS UI学习 ScrollView中Touch事件作用子视图
- iPhone开发中如何使UIWebView响应Touch事件
- IOS开发应用中NSDATA转换为NSSTRING时乱码问题解决
- IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例
- IOS开发:在Core Data中应用使用原生SQL功能
- 详解Cocos2d 开发关于CCLayer中Touch事件 以及优先级
- 详解Cocos2d CCLayer中Touch使用方法
- 详解Cocos2d CCLayer中Touch使用方法
- CCLayer中Touch事件
- Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理
- 关于touch事件优先级问题。
- iphone开发笔记——Cocos2d CCLayer中的touch
- Cocos2d CCLayer中的touch处理
- cocos2d学习-CCLayer中的touch
- cocos2dx CCLayer接受 touch 事件
- CCLayer中Touch事件(Standard Touch Delegate和Targeted Touch Delegate)
- cocos2d-x中关于touch事件的响应
- cocos2d-x中关于touch事件的响应
- cocos2d-x中关于touch事件的响应
- cocos2d-x 详解之 CCLayer(触摸事件)
- 游戏开发随手记:cocos2d ccLayer响应触摸事件方法
- cocos2d-x中用CCLayer实现对话框的简单做法,自动屏蔽下层Touch事件
- cocos2d-x-触屏(Touch)事件详解
- C语言测试:想成为嵌入式程序员应知道的0x10个基本问题
- 验证时出错。HRESULT = '8000000A'
- UNIX Domain Socket IPC (Linux C编程一站式学习)
- 霸气外漏的C++
- 【Visual Studio 2010】修改注册表解决误删除某一版本sdk后的编译故障
- 详解Cocos2d 开发关于CCLayer中Touch事件 以及优先级
- 原来写过的几篇题解
- HDU1247(字典树应用)
- boost1.50在VS2010下的配置
- Java实现字典树
- 对OpenHpi研究的一点心得随记
- 使用UNIX域函数进行套接字编程
- php 实例说明 socket通信机制
- 地方门户运营 最全思路10条