Swift3 当通过场景编辑器添加的同一个SpritekitNode工作时,以编程方式添加的SpritekitNode不会触发TouchsBegind

Swift3 当通过场景编辑器添加的同一个SpritekitNode工作时,以编程方式添加的SpritekitNode不会触发TouchsBegind,swift3,xcode8,ios10,Swift3,Xcode8,Ios10,我以编程方式向场景中添加了一个SKSpriteNode(name=building1),它不会启动touchsbegind函数。所以我尝试通过场景编辑器(使用相同的纹理)添加一个spritenode(name=building2,parent=GameScene),效果很好。我可以看到两座大楼,但只有第二座启动了触摸功能 class GameScene: SKScene { var build = SKSpriteNode() override func didMove(to view

我以编程方式向场景中添加了一个SKSpriteNode(name=building1),它不会启动touchsbegind函数。所以我尝试通过场景编辑器(使用相同的纹理)添加一个spritenode(name=building2,parent=GameScene),效果很好。我可以看到两座大楼,但只有第二座启动了触摸功能

class GameScene: SKScene {    
var build = SKSpriteNode()

override func didMove(to view: SKView) {

    build = SKSpriteNode(imageNamed: "build")
    build.name = "building1"
    build.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    build.position = CGPoint(x: 0, y: 0)
    build.zPosition = 0
    build.isUserInteractionEnabled = true
    self.addChild(build)

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("Something was touched")
    for touch in touches {
        let location = touch.location(in: self)

        if atPoint(location).name! == "building1" {
            print("\(atPoint(location).name!) was touched")
        }
        if atPoint(location).name! == "building2" {
            print("\(atPoint(location).name!) was touched")
        }
    }

}


}
类游戏场景:SKScene{
var build=SKSpriteNode()
覆盖func didMove(到视图:SKView){
build=SKSpriteNode(图像名为:“build”)
build.name=“building1”
build.ancorpoint=CGPoint(x:0.5,y:0.5)
build.position=CGPoint(x:0,y:0)
build.zPosition=0
build.isUserInteractionEnabled=true
self.addChild(构建)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
打印(“被触摸到的东西”)
接触{
让位置=触摸。位置(in:self)
如果在点(位置).name!=“building1”{
打印(\(触碰了atPoint(location.name!))
}
如果在点(位置)。名称!=“building2”{
打印(\(触碰了atPoint(location.name!))
}
}
}
}
奇怪的是,当我触摸building2(通过场景编辑器添加)时,我得到的输出是“某物被触摸了”“building2被触摸了”,而building1甚至没有启动该功能,即日志中没有显示任何内容。 我做错了什么?
谢谢

删除此行,它应该可以工作:

build.isUserInteractionEnabled = true
一开始可能看起来很奇怪。关键是让用户与节点交互,为什么我要禁用用户交互

好的,这就是文档中对isUserInteractionEnabled的说明:

指示节点是否接收触摸事件的布尔值

因此,如果我们将其设置为true,则节点(而不是场景)将接收触摸事件。因此,不会调用场景的
touchesbreated
方法。相反,将调用节点的
触摸开始
。但是,如果我们将其设置为false,节点将不会接收任何触摸事件。那么,触摸事件将走向何方?到节点的父节点(在本例中为场景)


默认情况下,您从编辑器中添加的节点具有
isUserInteractionEnabled=false

是,有效!!在这里,我想我已经完成了我的代码。ThanksI加了第二个雪碧,看看我是否错过了什么。不幸的是,我不认为isUserInteractionEnabled属性显示在属性检查器中。无论如何,这确实是一个微妙的问题,谢谢你花时间解释。