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

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

概述如何让自定义Layer触发触摸事件? bool LayerXXX::init(){  this->setTouchEnabled(true);  CCTouchDispatcher* td = CCDirector::sharedDirector()->getTouchDispatcher();  td->addTargetedDelegate(this, 0, true); //kCC

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


bool LayerXXX::init(){  this->settouchEnabled(true);  CCtouchdispatcher* td = CCDirector::sharedDirector()->gettouchdispatcher();  td->addTargetedDelegate(this,true); //kCcmenuHandlerPriority - 10  // ...}

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

CCtouchdispatcher中包含了两个CCtouchHandler的列表,
分别存储StandardtouchHandler和 TargetedtouchHandler。

属性:
this->mtouchPriporty

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

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

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


//-------------------------------//touch1 100//touch2 100touch1Layer* touch1layer = touch1Layer::create( ccc4f(255,128),100,100 );this->addChild( touch1layer );touch1layer->setposition(200,100);touch2Layer* touch2layer = touch2Layer::create( ccc4f(255,255,100 );this->addChild( touch2layer );touch2layer->setposition(250,100);//结果://touch2//touch1//-------------------------------//touch1 100//touch2 100touch2Layer* touch2layer = touch2Layer::create( ccc4f(255,100);touch1Layer* touch1layer = touch1Layer::create( ccc4f(255,100);结果:touch1touch2-------------------------------touch1 100touch2 99touch2Layer* touch2layer = touch2Layer::create( ccc4f(255,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

总结

以上是内存溢出为你收集整理的cocos2d-x触摸事件优先级的探究与实践全部内容,希望文章能够帮你解决cocos2d-x触摸事件优先级的探究与实践所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存