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);
            }
  • 相关阅读:
    怎么认Destsoon标签条件
    PHP将图片转base64格式函数
    修改Discuz!X系列开启防CC攻击,不影响搜索引擎收录
    discuz x3.2简化的搜索框代码
    让Discuz! X3.2 SEO标题里的“-”支持空格
    javascript的常用操作(二)
    Spring MVC中注解的简介
    Spring MVC + Thymeleaf
    Maven建立spring-web项目
    Spring @Autowired使用介绍
  • 原文地址:https://www.cnblogs.com/kenkofox/p/3974998.html
Copyright © 2011-2022 走看看