【cocos2d-x3.2】 模态对话框拦截所有事件

【cocos2d-x3.2】 模态对话框拦截所有事件,第1张

概述From: http://www.voidcn.com/article/p-tgakmibj-xy.html 开发基础:Cocos2dx 3.2 开发目标:1.实现模态对话框,无论d出多少层都可以拦截事件。 2.ScrollView上有按钮,可以点击按钮滑动,响应事件并且不影响模态对话框的事件拦截。 实现方法: 1.Cocos2dx 2版本中,我们会设置DlgLayer事件的优先级为-128,这样

From: http://www.jb51.cc/article/p-tgakmibj-xy.html


开发基础:Cocos2dx 3.2

开发目标:1.实现模态对话框,无论d出多少层都可以拦截事件。 2.ScrollVIEw上有按钮,可以点击按钮滑动,响应事件并且不影响模态对话框的事件拦截。



实现方法:
1.Cocos2dx 2版本中,我们会设置DlgLayer事件的优先级为-128,这样会拦截底层的按钮事件(-128),但是问题是会拦截当前层的按钮事件(-128),需要将DlgLayer上的点击事件手动传入对话框上的按钮处理。
3.0版本中已经不再使用了,这种方法有很大的局限性,尤其在界面比较多,比较复杂的情况下,传递事件是一种比较挫的做法。直接排除。

2.Cocos2dx 3.X版本中,因为改变了事件的规则,几乎所有的控件的事件处理都按SceneGraPHPriority走的,甚至连Cocos2dx 2.X版本的Menu的-128也更改为0了。
做法: 直接d出一个DlgLayer(CCcolorLayer),封装一个事件吞噬(ontouchBegan,ontouchended...),事件优先级为addEventListenerWithSceneGraPHPriority,即0
因为所有层的拦截事件优先级为0,就会先处理最上方的事件,即d出的对话框事件。


特殊情况:
d出的对话框上有ScrollVIEw,ScrollVIEw上有按钮,最常见的问题就是
1.点击按钮能触发事件,但是点击按钮无法滑动ScrollVIEw
解决办法:把按钮优先级设置为1(当前ScrollVIEw的优先级为0),这样就能点着按钮滑动了。
但是产生新问题,按钮能滑动,却不能点击了,因为DlgLayer的优先级为0,会拦截掉按钮的事件。

2.有一个特殊的API,setSwallowtouches,在为一个控件封装点击事件的时候,可以将setSwallowtouches(false)。
这就意味着按钮能够响应点击事件,但是因为没有吞噬事件,就可以把按钮的事件传给ScrollVIEw,ScrollVIEw也可以滑动。
DlgLayer 优先级 0
ScrollVIEw 优先级 0
button 优先级 0
根据ZOrder的顺序,先触发button,然后是ScrollVIEw,最后是DlgLayer

3. 3.0版本不建议修改priority,因为你无法得知需要d出多少层对话框,就无法确定每个对话框的priority

eventdispatcher事件分发规则
添加监听的方法addEventListenerWithSceneGraPHPriority和addEventListenerWithFixedPriority

1.SceneGraPHPriority
和Node节点绑定的所有事件的优先级为0,
添加监听器后,事件监听列表内部的排序为 "<0,scene graph (0 priority),>0"

2.FixedPriority
用来自己定制优先级,一般设置"<0 或 >0"

3. 响应事件的优先级,数值越小,越先响应,-1 0 1这个顺序
内部事件处理顺序:
1.优先级为负数的事件
2.优先级为0(scene graph)的事件,相同的优先级会根据Node的z顺序高的(绘制于顶部的)节点将优于z顺序低的节点。这将保证了诸如触碰事件的自顶向下传播。
3.优先级为整数的事件


最后附带一个lua版封装的touchableSprite

一个可点击的图片,可以在ScrollVIEw上点击滑动,响应事件。


[cpp] view plain copy print ? --[[ 可点击的图片,可以设置响应的优先级。 比如ScrollVIEw上的按钮,可以用touchableSprite实现。将touchableSprite设置为不吞噬消息即可。 ~~~lua --点击点击事件 localfunctionequip_touch_began_Listener(sender,touch,event) end --点击结束事件 localfunctionequip_touch_ended_Listener(sender,event) ifscroll_vIEw:istouchmoved()then return end --可以添加容错点击,如5个像素内。 localstart_pt=touch:getStartLocation() localend_pt=touch:getLocation() ifcheckint(start_pt.x)==checkint(end_pt.x)andcheckint(start_pt.y)==checkint(end_pt.y)then print(sender.ID_) print("Readytoequip......") end end --添加到ScrollVIEw上可点击的图片 localsp=touchableSprite.new("data/equip.png", equip_touch_began_Listener, equip_touch_ended_Listener, false) sp.ID_=9999 scroll_vIEw:addChild(sp) ~~~lua ]] localtouchableSprite=class("touchableSprite",function(pic_path) returndisplay.newSprite(pic_path) end) touchableSprite.__index=touchableSprite touchableSprite.Listener_=nil touchableSprite.swallowtouch_=true touchableSprite.fixedPriority_=0 touchableSprite.useNodePriority_=false touchableSprite.removeListenerOntouchended_=false touchableSprite.touch_began_Listener_=nil touchableSprite.touch_ended_Listener_=nil --[[ 构造一个可响应点击事件的touchableSprite。 ~~~lua --监听器有三个参数,第一个参数是自己 localfunctiontouch_began_Listener(sender,event) end localfunctiontouch_ended_Listener(sender,event) localstart_pt=touch:getStartLocation() localend_pt=touch:getLocation() ifcheckint(start_pt.x)==checkint(end_pt.x)andcheckint(start_pt.y)==checkint(end_pt.y)then print(sender.ID_) print("Readytoequip......") end end localsp=touchableSprite.new("data/equip.png", equip_touch_ended_Listener) sp:setTexture("data/equip1.png") sp:setPriority(1) parent:addChild(sp) ~~~lua @paramstringpic_path传入图片路径.可为空,后通过:setTexture("pic_path")重新设置 @paramtouch_began_Listener点击开始事件的监听器。为空时不响应。监听器有三个参数,第一个参数是可点击图片自己 @paramtouch_ended_Listener点击结束时事件监听器。为空时不响应。监听器有三个参数,第一个参数是可点击图片自己 @paramswallow_touch是否吞噬事件true事件不再向下传递false响应完事件后,继续向下传递 ]] functiontouchableSprite:ctor(pic_path,touch_began_Listener,touch_ended_Listener,swallow_touch) self.touch_began_Listener_=touch_began_Listener self.touch_ended_Listener_=touch_ended_Listener self.swallowtouch_=swallow_touch localfunctiononNodeEvent(event) ifevent=="enter"then self:onEnter() elseifevent=="exit"then self:onExit() end end self:registerScriptHandler(onNodeEvent) end functiontouchableSprite:settouchBeganListener(touch_began_Listener) self.touch_began_Listener_=touch_began_Listener end functiontouchableSprite:settouchendedListener(touch_ended_Listener) self.touch_ended_Listener_=touch_ended_Listener end functiontouchableSprite:onEnter() localeventdispatcher=self:getEventdispatcher() localfunctionontouchBegan(touch,event) locallocationInNode=self:convertToNodeSpace(touch:getLocation()) locals=self:getContentSize() localrect=cc.rect(0,s.wIDth,s.height) ifcc.rectContainsPoint(rect,locationInNode)then ifself.touch_began_Listener_then self:touch_began_Listener_(self,event) end returntrue end returnfalse end localfunctionontouchmoved(touch,event) end localfunctionontouchended(touch,event) print("ontouchended.......") ifself.touch_ended_Listener_then self:touch_ended_Listener_(self,event) end ifself.removeListenerOntouchended_then eventdispatcher:removeEventListener(self.Listener_) end end localListener=cc.EventListenertouchOneByOne:create() self.Listener_=Listener Listener:setSwallowtouches(self.swallowtouch_) Listener:registerScriptHandler(ontouchBegan,cc.Handler.EVENT_touch_BEGAN) Listener:registerScriptHandler(ontouchmoved,cc.Handler.EVENT_touch_MOVED) Listener:registerScriptHandler(ontouchended,cc.Handler.EVENT_touch_ENDED) if0==self.fixedPriority_then eventdispatcher:addEventListenerWithSceneGraPHPriority(Listener,self) else eventdispatcher:addEventListenerWithFixedPriority(Listener,self.fixedPriority_) end end functiontouchableSprite:setSwalllowtouch(swallow) self.swallowtouch_=swallow end functiontouchableSprite:onExit() localeventdispatcher=self:getEventdispatcher() eventdispatcher:removeEventListener(self.Listener_) end functiontouchableSprite:setPriority(fixedPriority) self.fixedPriority_=fixedPriority self.useNodePriority_=false end functiontouchableSprite:removeListenerOntouchended(toRemove) self.removeListenerOntouchended_=toRemove end functiontouchableSprite:setPriorityWithNode(useNodePriority) self.fixedPriority_=0 self.useNodePriority_=useNodePriority end returntouchableSprite 总结

以上是内存溢出为你收集整理的【cocos2d-x3.2】 模态对话框拦截所有事件全部内容,希望文章能够帮你解决【cocos2d-x3.2】 模态对话框拦截所有事件所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存