【问题标题】:Cocos2d JS / Cocos Creator add touch listener to specific nodeCocos2d JS / Cocos Creator 为特定节点添加触摸监听器
【发布时间】:2018-01-18 16:46:37
【问题描述】:

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

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

【问题讨论】:

    标签: javascript cocos2d-js


    【解决方案1】:

    是的,使用 Cocos Creator 你可以这样做:

    this.node.on(cc.Node.EventType.TOUCH_START, this.methodToBeCalled, this);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-24
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多