Cocos2d JS / Cocos Creator将触摸侦听器添加到特定节点

时间:2018-01-18 16:46:37

标签: javascript cocos2d-js

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

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 个答案:

答案 0 :(得分:1)

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

@implementation OCObject

+ (void)passArr: (NSArray *)arr {
    void (^aBlock)(NSString *) = arr[0];
    aBlock(@"Crash Here");
}

@end