• 事件响应的优先级、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);
            }
  • 相关阅读:
    安装MySQL-python时报错
    人的成功平台很重要
    开源运维工具
    遗忘Windows Server 2008R2密码的处理方法
    操作系统下载和操作系统更新失败解决
    说说对SQL 语句优化有哪些方法?
    Git彻底删除历史提交记录的方法
    MSSQL备份脚本
    .NET Core Data Access
    各种数据库默认端口总结
  • 原文地址:https://www.cnblogs.com/kenkofox/p/3974998.html
Copyright © 2020-2023  润新知