5.【cocos2d翻译系列】Basic concepts-touch input

来源:互联网 发布:软妹币是哪个直播软件 编辑:程序博客网 时间:2024/05/16 09:25

Touch Input


Cocos2d支持两种不同的处理触摸事件的方法。它们是两种不同的delegate(代理)定义。(都是在CCTouchDelegateProtocol.h中定义的)


Standard Touch Delegate(标准触摸代理)

[cpp] view plaincopyprint?
  1. @protocolCCStandardTouchDelegate <NSObject>  
  2. @optional  
  3. -(void)ccTouchesBegan:(NSSet *)touches  withEvent:(UIEvent*)event;  
  4. -(void)ccTouchesMoved:(NSSet *)touches  withEvent:(UIEvent*)event;  
  5. -(void)ccTouchesEnded:(NSSet *)touches  withEvent:(UIEvent*)event;  
  6. -(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event;  
  7. @end  

这是就是标准的触摸事件。你可以获得所有的触摸点的所有触摸事件。

CCLayer子类中为了获得这些事件,你只需设置isTouchEnabled= YES

[cpp] view plaincopyprint?
  1. self.isTouchEnabled= YES;  

Targeted Touch Delegate (定向触摸代理)

[cpp] view plaincopyprint?
  1. @protocolCCTargetedTouchDelegate <NSObject>  
  2. -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;@optional  
  3. // touch updates:  
  4. -(void)ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event;  
  5. -(void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event;  
  6. -(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;  
  7. @end  

注意此处和标准触摸代理有两处不同:

1.这些函数仅提供单点触摸。

2.ccTouchBegan方法要求返回一个布尔值。而且必须要声明ccTouchBegan才能使用后面的Moved,Ended,Cancelled方法

(全部是可选的)。

为了接受这些事件,你必须向全局dispatcher(派发器)注册一个定向触摸代理,在CCLayer的子类中,

重写registerWithTouchDispatcher如下:

[cpp] view plaincopyprint?
  1. -(void)registerWithTouchDispatcher {  
  2. [[CCTouchDispatchersharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];  
  3. }  

(你需要在文件开头处因为头文件”CCTouchDispatcher.h”

使用哪一个?

其实一般来说定向触摸代理(Targeted Touch Delegate)更易用,因为你不需要自己去分离NSSet,而且你也不用一直去判断这个

事件是否是你关心的。

但是如果你想在一个函数中处理多点触摸(例如:用手指旋转图片和缩放图片),那么标准的触摸代理是更好的选择。

注意:Note that you can only use one or the other.

MultiTouch(多点触摸)

为了接收多点触摸的事件,你需要激活它们。你可以在AppDelegate.m文件中的applicationDidFinishLaunching函数中

添加如下代码:

[cpp] view plaincopyprint?
  1. [glView setMultipleTouchEnabled:YES];  


Touch-enabling things that aren't CCLayers(允许非CCLayer的触摸事件)

上面讨论地isTouchEnableregisterWithTouchDispatcher仅适用于CCLayer和它的子类(显然CCMenu,已经为你做好了这一切)

若要其他的类允许触摸事件,有一些工作是必须要做的:

    1.这个类必须要实现适当地协议,CCStandardTouchDelegateCCTargetTouchDelegate这两者选其一。

    2.这个类必须在它onEnter函数中向dispatcter注册,在它onExit函数中撤销(当它是CCNode的子类时),或者其他类似的时间完

成这些事情(它不是CCNode的子类)。如果这个类已经在正在运行的scene中了,那你需要一些触摸事件就得立即注册或者注销了。


后面的那一条是让你自己手动处理的,在CCLayer中设置isTouchEnabled。有时我们会看到这样的代码:

[cpp] view plaincopyprint?
  1. //for CCStandardTouchDelegate  
  2. - (void)onEnter  
  3. {  
  4. [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0];  
  5. }  

[cpp] view plaincopyprint?
  1. //For CCTargetedTouchDelegate  
  2. - (void)onEnter  
  3. {  
  4. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];   
  5. }  

[cpp] view plaincopyprint?
  1. //And for both  
  2. - (void)onExit  
  3. {  
  4. [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];  
  5. [super onExit];  
  6. }  


问题注意

在你的触摸处理事件中使用CCLog或者NSLog是不好的,这会降低你程序的性能。

原创粉丝点击