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);
        }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-10-03
  • 2021-05-28
  • 2021-08-08
  • 2021-07-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
相关资源
相似解决方案