代码之家  ›  专栏  ›  技术社区  ›  jjuser19jj

Cocos2d-JS/Cocos-Creator将触控监听器添加到特定节点

  •  0
  • jjuser19jj  · 技术社区  · 8 年前

    我想在触摸时拖动一个特定的精灵。我想知道我是否只能将事件侦听器添加到特定节点,这样我就不必在为每个节点启动特定的拖动功能之前检查哪个节点被触碰了。当在特定节点外触碰时,以下代码也会“触发”。

    this.directionDial = new cc.Node()
        this.directionDial.graphics = this.directionDial.addComponent(cc.Graphics)
        this.node.addChild(this.directionDial)
    this.directionDial.graphics.lineWidth = 2;
        this.directionDial.graphics.strokeColor = cc.Color.RED;
        this.directionDial.graphics.circle(80, 0, 10);
        this.directionDial.graphics.stroke();
    
        var _this = this;
    
        // Touch control 
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {
                //do something
                _this.forceDirection = 1;
                _this.displayDirection()
                return true;
            }
        }, this.directionDial); 
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Bruno Lopes    8 年前

    是的,使用Cocos Creator,您可以执行以下操作:

    this.node.on(cc.Node.EventType.TOUCH_START, this.methodToBeCalled, this);
    
    推荐文章