Objective C - 如何在触摸第一个 SKnode 时禁用第二个 Sknode 的触摸

Objective C - How to disable touch on 2nd Sknode when 1st SKnode is touched

我有两个 SKNode,RMnode 和 RLnode, 接收触摸。不幸的是,与此同时。

我尝试在 第二个 SKnode 上禁用触摸, 当第一个被触摸时,反之亦然,但由于某种原因它似乎不起作用。

是否有另一种方法?

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
    CGPoint Rubyposition = [touch locationInNode:self];
    [self selectNodeForTouch:Rubyposition]; //a helper method that asks the scene (self) for the node that is on the position touchLocation.

    SKNode *RMnode = [self nodeAtPoint:Rubyposition];
    SKNode *RLnode = [self nodeAtPoint:Rubyposition];

    if ([RLnode.name isEqualToString:@"Ruby1"]) {
        if(_TouchOnRubyRL == NO){
            _TouchOnRubyRL = YES;
            //RMnode.userInteractionEnabled = NO; //Not working
            [self.level ActivatedBricks:_TouchOnRubyRL];
        }
        else if(_TouchOnRubyRL == YES){
            _TouchOnRubyRL = NO;
            //RMnode.userInteractionEnabled = YES; //not working
            [self.level ActivatedBricks:_TouchOnRubyRL];
        }
    }
    if ([RMnode.name isEqualToString:@"Ruby2"]) {
        if(_TouchOnRubyRM == NO){
            _TouchOnRubyRM = YES;
            //RLnode.userInteractionEnabled = NO; //Not working
            [self.level ActivatedBricks:_TouchOnRubyRM];
        }
        else if(_TouchOnRubyRM == YES){
            _TouchOnRubyRM = NO;
            //RLnode.userInteractionEnabled = YES; //Not working
            [self.level ActivatedBricks:_TouchOnRubyRM];
        }
    }
}
}

设置节点的 userInteractionEnabled 没有像您预期的那样工作的原因是您的 touchesBegan 在场景中,而不是精灵中。 userInteractionEnabled 仅 allows/disallows 触及 SKNode 它是 属性 的(SKScene 也是 SKNode) - 在您的代码中,场景的 userInteractionEnabled 始终为真,因此触摸始终通过场景的 touchesBegan 方法(你的 sprite 的 touchesBegan 被禁用 - 但你没有覆盖它所以你没有注意到)。

要以这种方式使用 userInteractionEnabled,您需要将 sprite 本身子类化并在子类化的 sprite 中处理触摸 - 然后设置 userInteractionEnabled 将按您预期的方式工作。

如果您不想那样重构您的代码,您可以用其他方式将每个精灵标记为 'untouchable'(this SO answer 建议使用 SKNode 现有的 zPosition 属性)然后在接受触摸之前在您的场景中使用它(例如,如果 touchedNode.zPosition != 0 ...)。