我正试图通过书籍2D iOS& amp;由Ray Wenderlich拍摄的tvOS游戏和我的游戏崩溃的原因是“试图添加一个已经有父母的SKNode”,因为孩子已经在现场并且循环尝试再次添加它,但我不知道如何修复它
谢谢!
func backgroundNode() -> SKSpriteNode {
// 1
let backgroundNode = SKSpriteNode()
backgroundNode.anchorPoint = CGPoint.zero
backgroundNode.name = "background"
// 2
background1.anchorPoint = CGPoint.zero
background1.size = self.frame.size
background1.position = CGPoint(x: 0, y: 0)
backgroundNode.addChild(background1)
// 3
background2.anchorPoint = CGPoint.zero
background1.size = self.frame.size
background2.position =
CGPoint(x: background1.size.width, y: 0)
backgroundNode.addChild(background2)
// 4
backgroundNode.size = CGSize(
width: background1.size.width + background2.size.width,
height: background1.size.height)
return backgroundNode
}
for i in 0...1 {
let background = backgroundNode()
background.anchorPoint = CGPointZero
background.position =
CGPoint(x: CGFloat(i)*background.size.width, y: 0)
background.name = "background"
addChild(background)
}
答案 0 :(得分:0)
您在for循环中循环两次,因此您需要将同一个孩子添加两次。
将for循环的第一行更改为:for i in 0..<1 {
使用for i in x...y
时,您从x开始,然后转到并包含 y。