zoukankan      html  css  js  c++  java
  • 事件响应的优先级、stopProgapation禁止下层组件响应

    cocos2d-js没有完整的鼠标事件处理,这点比js/flash的要差一些,不过凑合着也可以用了。
    一般界面编程,可以用显示列表的Node作为监听器的优先级,在上方的会比下方的高优先级。
    而cocos2d-js没有stopImmediatePropagation,只有stopProgapation,一旦某个监听器中执行了stopProgapation,后续的监听器都不会被执行。这里并没有js/flash的冒泡概念。
     
    如果在上层Node中stopProgapation,那么效果就有点像设置了swallowTouches:true,但会更灵活
     
    例子:
     
    界面上添加2个sprite,child1在下,child2在上。
    如下的代码,child2的监听器优先级高,会首先执行,其中func2会先输出,因为按顺序执行,但由于stopProgapation,所以child1的监听器不会被执行。
     
            if("touches" in cc.sys.capabilities){
                cc.eventManager.addListener({event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function(){
                    trace("func1");
                    return true;
                }}, this.child1);
                cc.eventManager.addListener({event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function(touch,event){
                    trace("func2");     //按顺序执行,先func2,再func3
                    return true;
                }}, this.child2);
                cc.eventManager.addListener({event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function(touch,event){
                    trace("func3");
                    event.stopPropagation();
                    return true;
                }}, this.child2);
            }else{
                cc.eventManager.addListener({event: cc.EventListener.MOUSE, onMouseDown: function(){
                    trace("func1");
                }}, this.child1);
                cc.eventManager.addListener({event: cc.EventListener.MOUSE, onMouseDown: function(event){
                    trace("func2");     //按顺序执行,先func2,再func3
                }}, this.child2);
                cc.eventManager.addListener({event: cc.EventListener.MOUSE, onMouseDown: function(event){
                    trace("func3");
                    event.stopPropagation();
                }}, this.child2);
            }
  • 相关阅读:
    iframe自适应高度的多种方法方法
    [tomcat][mysql][JDK][maven][myEclipse][ubuntu][centOs][oracle]等下载地址整理
    解决电脑速度越来越慢的方法
    li:hover 上再IE6下就没有效果的解决方法
    各种常用文件后缀名详解
    ASP.net中网站访问量统计方法
    RabbbitMQ的配置 以在.NetCore 的CAP 使用RabbbitMQ 实现订阅与发布问题
    .netCore 根据模型生成数据库
    jsonp Ajax跨域请求
    IE8 placeholder兼容+Password兼容
  • 原文地址:https://www.cnblogs.com/kenkofox/p/3974998.html
Copyright © 2011-2022 走看看