CCLayer中Touch事件(Standard Touch Delegate和Targeted Touch Delegate)
2014-05-18 22:08
393 查看
在做练习时,触控失灵,看源码了解下触控事件.
练习中的操作:直接在CClayer子类的init里 this->setTouchEnabled(true);
覆盖的事件处理方法
结果按键没反应
因为setTouchEnabled(true); 开启多点触摸, 而事件处理方法是针对单点的,所以不行.
解决方法1.
覆盖事件多点处理方法
从参数类型CCSet可以看出此参数是集合,应该是多个按的点.
解决方法2.
覆盖onEnter(),加上单点事件委托
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];
关于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子类的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];
addTargetedDelegate:self中的swallowsTouches
关于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)
- CCTouchDelegateProtocol(CCTouchDelegate触摸事件委托 两子类:1CCTargetedTouchDelegate,2CCStandardTouchDelegate)
- cocos2d ccLayer响应触摸事件方法:CCStandardTouchDelegate 与 CCTargetedTouchDelegate
- (Standard Touch Delegate 和 Targeted Touch Delegate)CCLayer中的两种Touch类型
- cocos2d ccLayer响应触摸事件方法:CCStandardTouchDelegate 与 CCTargetedTouchDelegate
- cocos2d ccLayer响应触摸事件方法:CCStandardTouchDelegate 与 CCTargetedTouchDelegate
- Cocos2d-x之Touch事件处理机制 提供两种触摸事件处理机制:CCStandardTouchDelegate和CCTargetedTouchDelegate。
- Cocos-2d CCLayer的触摸响应CCTouchDelegate和CCStandardTouchDelegate 和 CCTargetedTouchDelegate
- Cocos-2d CCLayer的触摸响应CCTouchDelegate和CCStandardTouchDelegate 和 CCTargetedTouchDelegate
- Cocos-2d CCLayer的触摸响应CCTouchDelegate和CCStandardTouchDelegate 和 CCTargetedTouchDelegate
- cocos2d ccLayer响应触摸事件方法:CCStandardTouchDelegate 与 CCTargetedTouchDelegate
- Cocos-2d CCLayer的触摸响应CCTouchDelegate和CCStandardTouchDelegate 和 CCTargetedTouchDelegate
- 对于CCLayer中对touch事件不响应的问题解析
- Android事件分发07——TouchDelegate的使用与解析
- cocos2dx 不用直接在 onEnter 里面 addTargetedDelegate (或 addStandardDelegate)
- Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理
- Android之View的TouchDelegate,你真的理解事件分发了吗???
- cocos2dx 不要直接在 onEnter 里面 addTargetedDelegate (或 addStandardDelegate)
- cocos2d-x touches事件中使用 addTargetedDelegate 的潜在bug
- cocos2dx之CCTouchTargetedDelegate、CCTouchStandardDelegate 吞噬触屏