Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当isUserInteractionEnabled为false时,SKSpriteNode不允许触摸通过_Ios_Swift_Sprite Kit_Touch - Fatal编程技术网

Ios 当isUserInteractionEnabled为false时,SKSpriteNode不允许触摸通过

Ios 当isUserInteractionEnabled为false时,SKSpriteNode不允许触摸通过,ios,swift,sprite-kit,touch,Ios,Swift,Sprite Kit,Touch,我正在尝试使用SKSpriteNode在SpriteKit中创建一个覆盖。但是,我希望触摸通过覆盖,因此我将isUserInteractionEnabled设置为false。然而,当我这样做时,SKSpriteNode似乎仍然吸收了所有的接触,并且不允许底层节点做任何事情 下面是我所说的一个例子: class TouchableSprite: SKShapeNode { override func touchesBegan(_ touches: Set<UITouch>,

我正在尝试使用
SKSpriteNode
在SpriteKit中创建一个覆盖。但是,我希望触摸通过覆盖,因此我将
isUserInteractionEnabled
设置为false。然而,当我这样做时,
SKSpriteNode
似乎仍然吸收了所有的接触,并且不允许底层节点做任何事情

下面是我所说的一个例子:

class 

TouchableSprite: SKShapeNode {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Touch began")
    }
}

class GameScene: SKScene {
    override func sceneDidLoad() {
        // Creat touchable
        let touchable = TouchableSprite(circleOfRadius: 100)
        touchable.zPosition = -1
        touchable.fillColor = SKColor.red
        touchable.isUserInteractionEnabled = true
        addChild(touchable)


        // Create overlay
        let overlayNode = SKSpriteNode(imageNamed: "Fade")
        overlayNode.zPosition = 1
        overlayNode.isUserInteractionEnabled = false
        addChild(overlayNode)
    }
}
类
可触摸精灵:SKShapeNode{
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
打印(“触摸开始”)
}
}
类游戏场景:SKScene{
重写func sceneDidLoad(){
//创造可触摸的
让可触摸=可触摸精灵(圆圈半径:100)
touchable.zPosition=-1
touchable.fillColor=SKColor.red
touchable.isUserInteractionEnabled=true
addChild(可触摸)
//创建覆盖
让重叠节点=SKSpriteNode(图像名为“淡入”)
overlynode.zPosition=1
OverlyNode.isUserInteractionEnabled=false
addChild(重叠节点)
}
}
我尝试过将
SKSpriteNode
子类化,并手动将触摸事件委托给适当的节点,但这会导致许多奇怪的行为


任何帮助都将不胜感激。谢谢

阅读您找到的实际来源:

/**
  Controls whether or not the node receives touch events
*/
open var isUserInteractionEnabled: Bool
但这是指触摸的内部节点方法。 默认情况下,
isUserInteractionEnabled
false那么,在覆盖
SKSpriteNode>这样的子对象上的触摸,默认情况下,是处理到主(或父)类的简单触摸(对象在这里,存在,但如果没有实现任何操作,只需触摸它)

要通过触摸浏览覆盖图,您可以在
游戏场景
中实现如下代码。请记住不要将-1用作
zPosition
,因为这意味着它位于场景下方

p.S.:我已经在你的精灵中添加了一个
名称
,以便在
触摸开始时调用它,但是你可以为你的
游戏场景创建全局变量

override func sceneDidLoad() {
        // Creat touchable
        let touchable = TouchableSprite(circleOfRadius: 100)
        touchable.zPosition = 0
        touchable.fillColor = SKColor.red
        touchable.isUserInteractionEnabled = true
        touchable.name = "touchable"
        addChild(touchable)
        // Create overlay
        let overlayNode = SKSpriteNode(imageNamed: "fade")
        overlayNode.zPosition = 1
        overlayNode.name = "overlayNode"
        overlayNode.isUserInteractionEnabled = false
        addChild(overlayNode)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("GameScene Touch began")
        for t in touches {
            let touchLocation = t.location(in: self)
            if let overlay = self.childNode(withName: "//overlayNode") as? SKSpriteNode {
                if overlay.contains(touchLocation) {
                    print("overlay touched")
                    if let touchable = self.childNode(withName: "//touchable") as? TouchableSprite {
                        if touchable.contains(touchLocation) {
                            touchable.touchesBegan(touches, with: event)
                        }
                    }
                }
            }
        }
    }
}
override func sceneDidLoad(){
//创造可触摸的
让可触摸=可触摸精灵(圆圈半径:100)
touchable.zPosition=0
touchable.fillColor=SKColor.red
touchable.isUserInteractionEnabled=true
touchable.name=“touchable”
addChild(可触摸)
//创建覆盖
让重叠节点=SKSpriteNode(图像名为“淡入”)
overlynode.zPosition=1
overlynode.name=“overlynode”
OverlyNode.isUserInteractionEnabled=false
addChild(重叠节点)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
打印(“游戏场景触摸开始”)
对于t-in-touch{
让touchLocation=t.location(in:self)
如果let overlay=self.childNode(名称://overlayNode)作为?SKSpriteNode{
如果覆盖.contains(触摸位置){
打印(“覆盖触摸”)
如果让touchable=self.childNode(名称://touchable)作为?TouchableSprite{
如果可触摸。包含(触摸位置){
可触摸。触摸开始(触摸,带有:事件)
}
}
}
}
}
}
}

您需要小心使用zPosition。有可能走到幕后,使事情复杂化

更糟糕的是触摸事件的顺序。我记得读到它是基于节点树的,而不是zPosition。禁用场景触摸,您应该可以看到结果

现在它是这么说的:

当发生以下情况时,命中测试顺序与场景中的绘图顺序相反: SpriteKit处理触摸或鼠标事件,它在场景中漫游以查找 要接受事件的最近节点。如果该节点没有 要获取事件,SpriteKit将检查下一个最近的节点,等等。这个 命中测试的处理顺序基本上与 绘图顺序

对于命中测试期间要考虑的节点,其 userInteractionEnabled属性必须设置为“是”。默认值 对于除场景节点之外的任何节点,都没有。要接收数据的节点 事件需要从其自身实现适当的响应程序方法 父类(iOS上的UIResponder和OS X上的nsrresponder)。这是一个 您必须在中实现特定于平台的代码的少数几个地方 斯皮特基特


但情况可能并非总是如此,因此您需要检查8、9和10之间的一致性

您的zposition位于场景下方,因此您的zorder是overlay->scene->Touchable这是一种智慧、轻微的喜剧,以及一个处于最佳状态的人的尖锐评论。非常感谢。