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

CCTouchDispatcher中包括了两个CCTouchHandler的列表,

分别存储StandardTouchHandler和 TargetedTouchHandler。



属性:

this->mTouchPriporty

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



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

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

-------------------------------

Touch1 100

Touch2 100



Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(255,0,0,128), 100, 100 );

this->addChild( touch1layer );

touch1layer->setPosition(200, 100);





Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(255,255,0,128), 100, 100 );

this->addChild( touch2layer );

touch2layer->setPosition(250, 100);





结果:

Touch2

Touch1

-------------------------------

Touch1 100

Touch2 100

Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(255,255,0,128), 100, 100 );

this->addChild( touch2layer );

touch2layer->setPosition(250, 100);





Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(255,0,0,128), 100, 100 );

this->addChild( touch1layer );

touch1layer->setPosition(200, 100);





结果:

Touch1

Touch2

-------------------------------

Touch1 100

Touch2 99

Touch2Layer* touch2layer = Touch2Layer::create( ccc4f(255,255,0,128), 100, 100 );

this->addChild( touch2layer );

touch2layer->setPosition(250, 100);





Touch1Layer* touch1layer = Touch1Layer::create( ccc4f(255,0,0,128), 100, 100 );

this->addChild( touch1layer );

touch1layer->setPosition(200, 100);





结果:

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, 1);

// 设置优先级

CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority - 2, this);

}









void Touch1Layer::loseAllFocus()

{

// 获取顶层的全部节点

CCArray* arrChilds = SceneController::GetInstancePtr()->getCurLayer()->getChildren();





for(int i=0; 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, 0);

CCTouchDispatcher::sharedDispatcher()->setPriority(kCCMenuTouchPriority - 1, 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

cocos2dx-lua 触摸锁定自身以外的层

2013-09-02 11:58:15|  分类:
LUA |  标签:
|举报
|字号大中小 订阅

self中来继承于CCLayer

1、首先在初始化的时候要注冊触摸事件:

local listener = function(eventType, x, y)
       -- log.debug(eventType.. "="..x)
        if eventType == CCTOUCHBEGAN then
            return self:touch_began(x, y)
        elseif eventType == CCTOUCHMOVED then
            self:touch_moved(x, y)
        elseif eventType == CCTOUCHENDED then
            self:touch_ended(x, y)
        elseif eventType == CCTOUCHCANCELLED then
            self:touch_cancelled(x, y)
        end
    end
    self:registerScriptTouchHandler(listener, false, -999999999, true)
--注意,这儿第四个參数要设置为true,第三个參数为响应优先级,详细请參看文档

2、打开当前所在layer的触摸事件
self:setTouchEnabled(true)

3、在開始触摸的时候返回true
function HelloWorldLayer:touch_began(x, y)
    return true
end

这样之后,就仅仅有当前的layer能够响应触控事件了。

分享到: 


cocos2d-x触摸事件优先级的更多相关文章

  1. cocos2d-x触摸事件优先级的探究与实践

    如何让自定义Layer触发触摸事件? bool LayerXXX::init() { this->setTouchEnabled(true); CCTouchDispatcher* td = C ...

  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. iOS中响应者链条-触摸事件

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

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

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

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

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

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

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

  9. cocos2d-x jsb 防止触摸事件传递

    在游戏中要实现消息弹窗,让用户点击确认,其他区域产生遮罩,阻挡下层的事件被点击到,这是个很常用的功能,在cocos2d-x中,可以通过为layer添加事件代理来实现: pDirector->ge ...

随机推荐

  1. redis 内存管理与数据淘汰机制(转载)

    原文地址:http://www.jianshu.com/p/2f14bc570563?from=jiantop.com 最大内存设置 默认情况下,在32位OS中,Redis最大使用3GB的内存,在64 ...

  2. 数据库质疑或只有MDF文件资料

    --允许进行系统表的操作 use master declare @databasename varchar(255) set @databasename='Blwy BarCode' --1.如果用户 ...

  3. nginx的详解(四)

    10.nginx的访问控制及DDOS预防1)访问控制配置基于各种原因,Ningx有时要进行访问控制.比如说,一般网站的后台都不能让外部访问,所以要添加 IP 限制,通常只允许公司的IP访问.访问控制就 ...

  4. 【bzoj3689】异或之 可持久化Trie树+堆

    题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...

  5. BZOJ 1113 Wall ——计算几何

    凸包第一题. 自己认为自己写的是Andrew 其实就是xjb写出来居然过掉了测试. 刚开始把pi定义成了int,调了半天 #include <map> #include <cmath ...

  6. 【CCF】行车路线 改编Dijkstra

    [AC] #include<iostream> #include<cstdio> #include<string> #include<cstring> ...

  7. 【线段树区间最值单点更新模板】BNUOJ 52965 E Excellent Engineers

    http://acm.bnu.edu.cn/v3/external/gym/101512.pdf #include<bits/stdc++.h> using namespace std; ...

  8. SpringBoot消失的Web.xml

    Filter 过滤器作为web.xml中重要的一部分,有着相当高的出场率,SpringBoot会默认注册几个Filter ApplicationContextHeaderFilter Characte ...

  9. 树状数组求第K大(From CLJ)

    ; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;

  10. hdu 5012 bfs 康托展开

    Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...