Swift 更改for循环中SKNode子级的属性
我知道SKNode的children属性是只读属性。但是,在迭代子对象时,是否有可能更改这些子对象的属性 例如:Swift 更改for循环中SKNode子级的属性,swift,for-loop,sprite-kit,sknode,Swift,For Loop,Sprite Kit,Sknode,我知道SKNode的children属性是只读属性。但是,在迭代子对象时,是否有可能更改这些子对象的属性 例如: let background = SKSpriteNode(imageNamed: "Background.jpg") for child in background.children{ child.position = CGPoint(x: 0, y: 0) } 您可以将子节点强制转换为SKNode,然后更改位置 for child in self.childre
let background = SKSpriteNode(imageNamed: "Background.jpg")
for child in background.children{
child.position = CGPoint(x: 0, y: 0)
}
您可以将子节点强制转换为
SKNode
,然后更改位置
for child in self.children {
if let sprite = child as? SKNode {
sprite.position = CGPoint(x: 0, y: 0)
}
}
children属性是只读的,因此您不能使用此属性添加或删除元素(子元素)(有特定的方法可以执行此操作) 但是,您可以修改background.children中元素的某些属性
for child in background.children as [SKNode] {
child.position = CGPoint(x: 0, y: 0)
}
将background.children强制转换为[SKNode]是安全的,并由