如何让自定义Layer触发触摸事件?

bool LayerXXX::init()
{
  this->setTouchEnabled(true);   CCTouchDispatcher* td = CCDirector::sharedDirector()->getTouchDispatcher();
  td->addTargetedDelegate(this, , true); //kCCMenuHandlerPriority - 10   // ...
}

CCTouchDispatcher是管理cocos2d-x中所有Touch事件派发的类,

CCTouchDispatcher中包含了两个CCTouchHandler的列表,
分别存储StandardTouchHandler和 TargetedTouchHandler。

属性:
this->mTouchPriporty

Layer 优先级越小越高
越低越先响应事件

实验一:当两个Layer优先级同等的时候会怎么样呢?

实验发现,同等优先级下,后添加的Layer先响应事件。

//-------------------------------
//Touch1 100
//Touch2 100
Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(,,,), , );
this->addChild( touch1layer );
touch1layer->setPosition(, ); Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(,,,), , );
this->addChild( touch2layer );
touch2layer->setPosition(, ); //结果:
//Touch2
//Touch1
//-------------------------------
//Touch1 100
//Touch2 100
Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(,,,), , );
this->addChild( touch2layer );
touch2layer->setPosition(, ); Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(,,,), , );
this->addChild( touch1layer );
touch1layer->setPosition(, ); 结果:
Touch1
Touch2
-------------------------------
Touch1
Touch2
Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(,,,), , );
this->addChild( touch2layer );
touch2layer->setPosition(, ); Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(,,,), , );
this->addChild( touch1layer );
touch1layer->setPosition(, ); //结果:
//Touch2
//Touch1
//说明优先级越小越先触发事件
//-------------------------------

如何阻塞事件的向后传递?

原理:
mSwallowsTouches = false的时候,该层的touch事件若接受处理后,touch事件穿透,进入下个注册touch事件的layer进行处理

若mSwallowsTouches = true时,当该层处理touch事件的时候,若bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
return true时候,则touch事件被该层接收走,其他优先级较低的,就不会接收到touch事件的处理申请了。

关于ccTouchBegan的返回值
true:
本层的后续Touch事件可以被触发,并阻挡向后层传递
false:
本层的后续Touch事件不能被触发,并向后传递

总结:

如何阻塞事件的向后传递?

主要是利用了TargetedTouchDelegate 的一个叫SwallowTouch的参数 ,如果这个开关打开的话,

比他权限低的handler 是收不到 触摸响应的,这里的权限低的意思是先看priority(priority越低的优先级越高)再看哪个Layer最后addChild进去(越后添加的优先级越高)。

CCMenu 就是开了Swallow 并且权限为-128(权限是越小越好),所以CCMenu的事件不会出现击穿

mSwallowsTouches = true 并且 ccTouchBegan 返回 true

如何让Layer所有触摸同时穿透Begin、Move、End事件?

mSwallowsTouches = false 并且 ccTouchBegan 返回 true

ccTouchBegan 返回 true 表示同层处理后续事件(吞噬)
ccTouchBegan 返回 false 表示同层不处理后续事件(Move End Cancled)  (击穿)

mSwallowsTouches 设为 true 表示触摸不向下层传递(不一定 如mSwallowsTouches为true began返回false还是会向后传递)
mSwallowsTouches 设为 false 表示触摸向下层传递(不知有啥用)

this->mTouchPriporty 越小,越先接收到触摸
this->mTouchPriporty 同等,越后addChild的越先响应

如何管理多个对话框的优先级?

事件的优先级和绘图的优先级的关系和区别?

VertexZ 又是什么?(VertexZ是openGl的z轴)

绘图的优先级叫ZOrder

如何改版绘图的优先级?

如在容器中通过调用
this->reorderChild(CCNode* child, int zOrder);

如何设置触摸事件的优先级?
CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority - 1, layer);

如何得到触摸事件的优先级?
this->mTouchPriporty (CCNode类成员 私有变量)

如何遍历容器获取特定的对象??

void Touch1Layer::setFocus()
{
  // 将zorder=1; priority= kCCMenuTouchPriority - 2;   // 设置zorder
  SceneController::GetInstancePtr()->getCurLayer()->reorderChild(this, );
  // 设置优先级
  CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority - , this);
} void Touch1Layer::loseAllFocus()
{
  // 获取顶层的所有节点
  CCArray* arrChilds = SceneController::GetInstancePtr()->getCurLayer()->getChildren();   for(int i=; i< arrChilds->count(); i++)
  {
    CCLayerColor* layer = dynamic_cast< CCLayerColor* >( arrChilds->objectAtIndex(i) );     // 跳过自己(不撤销自己的优先级)
    if(layer != NULL && layer != this)
    {
      // 将zorder=0; priority= kCCMenuTouchPriority - 1;
      SceneController::GetInstancePtr()->getCurLayer()->reorderChild(layer, );
      CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority - , layer);
    }
  }
}

如何判断点在矩形内部?

CCPoint pos = this->getPosition();
CCSize size = this->getContentSize();
CCRect rect(pos.x, pos.y, size.width, size.height); if( CCRect::CCRectContainsPoint(rect, point) )
{
}

z值大的成员在z值小的成员的上面;

官方解释:

Differences between openGL Z vertex and cocos2d Z order:
   - OpenGL Z modifies the Z vertex, and not the Z order in the relation between parent-children
   - OpenGL Z might require to set 2D projection
   - cocos2d Z order works OK if all the nodes uses the same openGL Z vertex. eg: vertexZ = 0

@warning: Use it at your own risk since it might break the cocos2d parent-children z order

cocos2d-x触摸事件优先级的探究与实践的更多相关文章

  1. cocos2d-x触摸事件优先级

     CCTouchDispatcher是管理cocos2d-x中全部Touch事件派发的类, CCTouchDispatcher中包括了两个CCTouchHandler的列表, 分别存储Standa ...

  2. Cocos2D v3.x中关于重叠触摸层优先级的问题

    在Cocos2D v2.x版本中可以通过以下方法设置本层的触摸优先级: [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate ...

  3. cocos2dx中的触摸事件及触摸优先级

    1.只有CCLayer及其派生类才有触摸功能. 2.开启触摸 setTouchEnable(true); 3.设置触摸模式,单点,多点(仅IOS支持) setTouchMode(kCCTouchesO ...

  4. Cocos2d-x示例:单点触摸事件

    为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例.使用事件触发器实现单点触摸事件.该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们. ...

  5. cocos2d-x游戏引擎核心之五——触摸事件和触摸分发器机制

    一.触摸事件 为了处理屏幕触摸事件,Cocos2d-x 提供了非常方便.灵活的支持.在深入研究 Cocos2d-x 的触摸事件分发机制之前,我们利用 CCLayer 已经封装好的触摸接口来实现对简单的 ...

  6. iOS中响应者链条-触摸事件

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  7. Cocos2d-android (06) 屏幕触摸事件及坐标转换

    为屏幕添加触摸事件,将左上角坐标转换为左下角坐标 package com.arlen.cocos2d.touch01; import org.cocos2d.layers.CCLayer; impor ...

  8. cocos2d-x 详解之 CCLayer(触摸事件)

    CCLayer继承自CCNode,在CCLayer中可以实现单点触摸.多点触摸和重力感应回调3种不同形式的交互.这部分的难点在于,当存在多个层都要去接收触摸时它的响应机制是如何处理的.了解内部的处理机 ...

  9. Cocos2d-x实例:单点触摸事件

    addChild(boxC,30, kBoxC_Tag);                                                                        ...

随机推荐

  1. TSS 任务状态段

    TSS(任务状态段) 1 什么是TSS TSS 全称task state segment,是指在操作系统进程管理的过程中,任务(进程)切换时的任务现场信息.  2 TSS工作细节 TSS在任务切换过程 ...

  2. [水题]Codeforces337A Puzzles

    题目链接 题意:要在m个数里面选n个数, 要求这n个数的差值要最小 题意在hint里很清晰了 这道题从题意到题目本身都没有什么trick 写这道题完全是为了用一下#include <numeri ...

  3. Linux设备驱动程序:中断处理之顶半部和底半部

    http://blog.csdn.net/yuesichiu/article/details/8286469 设备的中断会打断内核中进程的正常调度和运行,系统对更高吞吐率的追求势必要求中断服务程序尽可 ...

  4. VirtualBox虚拟vdi磁盘扩容

    http://blog.chinaunix.net/uid-25627207-id-3342576.html

  5. Altium Designer完美双屏显示方法演示

    布线时我们往往需要对一些信号线做特别的走线处理,这样需要边布线边对照原理图,在protel99中那是一个很痛苦的事,在Altium Designer中这种情况将变很简单. 硬件要求,笔记本+外接显示器 ...

  6. CDC 虚拟com口

    现在很多电脑已经不带232了,特别是手提电脑.这使很多使用手提在外调试人员非常不方便.或许你可以买一条市面上usb转232转换线,但这些线抗干扰不是太好,在一些干扰大的地方会发生连接中断的问题,所以往 ...

  7. 保持与 Microsoft Azure Files 的连接

    我们在最近的博客文章中介绍了 Azure StorageFiles的预览版,请单击此处.该文章包含 Azure Files 的相关信息,说明了如何申请预览版并开始使用,还介绍了一些有助于创建共享和传 ...

  8. 嵌入式开发软件环境:uboot、kernel、rootfs、data布局分析

    uboot+linux的整体方案 开发板的datasheet中都有详细的地址空间的划分,其中比较重要的两块是:DDR地址空间和Flash地址空间.DDR空间是系统和应用的运行空间,一般由linux系统 ...

  9. MongoDB之四( 索引操作)

    我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又会花费50%的时间在R上面,因为Read操作对用户来说是非常敏感的,处理不好就会被人唾弃,呵呵. 从算法上来说有 ...

  10. [liu yanling]软件测试的过程

    测试过程按4个步骤进行,即单元测试.组装测试.确认测试和系统测试.