• 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处理方法,例如以下:

    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.  
    3. -(void) registerWithTouchDispatcher {   
    4.        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];  
    5.  } 

    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接受。其他类仍然能够接受。


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    python3笔记二十二:正则表达式之函数
    python3笔记二十一:时间操作datetime和calendar
    python3笔记二十:时间操作time
    python3笔记十七:python文件读写
    Spring常用注解
    Pytorch实现卷积神经网络CNN
    Keras实现autoencoder
    Keras实现LSTM
    TensorFlow实现CNN
    Recurrent Neural Networks vs LSTM
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4634761.html
Copyright © 2020-2023  润新知