swift:返回位置相同的子节点

swift: returning a child node with the same position

我在 swift 文件的函数中有两个 Sprite 节点。两个节点的大小和位置都相似。 Node2 是节点的子节点。

场景中添加节点时,节点位置各不相同。他们并没有像他们应该的那样在彼此之上。

func stoppers()->SKSpriteNode{   
 let node=SKSpriteNode(imageNamed:"image1")
    node.size=CGSize(width:20,height:300)
    node.position=CGPoint(x:100,y:100)

    let node2=SKSpriteNode(imageNamed:"image2")
    node2.size=CGSize(width:20,height:300)
    node2.position=CGPoint(x:100,y:100)

node.addChild(node2)
return node
}

函数调用场景如下: addChild(game.stoppers())

如果 node2node1 的子代并且您希望它们在屏幕上的相同位置,则 node2 必须具有 position (x: 0, y: 0)。这是因为子节点的位置是相对于父节点的。他们还需要具有相同的 anchorPoint.