更改 Swift 中子节点的大小
Change size of a childnode in Swift
我有 2 个 SKSprite 节点:机器人和计算机
robot 是 parentNode computer 是 childNode
robot.addChild(computer)
现在我想通过使用父节点的名称来更改计算机的大小。
所以我需要这样的代码:robot.childnode.size.width = xxx
我该怎么做?
原因:我有多个称为机器人的 skspritenode,我可以通过碰撞检测它们是哪一个,所以我需要此代码来访问该特定父节点的子节点。
在将节点添加到 robot
时为 computer
节点设置名称。
computer.name = "computer"
robot.addChild(computer)
稍后你可以写...
if let computer = robot.childNodeWithName("computer") as? SKSpriteNode {
// you can install El Capitain and change the properties of computer here
}
...或者如果您更喜欢单行版本:
(robot.childNodeWithName("computer") as? SKSpriteNode)?.size.height = 100
希望这对您有所帮助。
我有 2 个 SKSprite 节点:机器人和计算机 robot 是 parentNode computer 是 childNode
robot.addChild(computer)
现在我想通过使用父节点的名称来更改计算机的大小。 所以我需要这样的代码:robot.childnode.size.width = xxx 我该怎么做?
原因:我有多个称为机器人的 skspritenode,我可以通过碰撞检测它们是哪一个,所以我需要此代码来访问该特定父节点的子节点。
在将节点添加到 robot
时为 computer
节点设置名称。
computer.name = "computer"
robot.addChild(computer)
稍后你可以写...
if let computer = robot.childNodeWithName("computer") as? SKSpriteNode {
// you can install El Capitain and change the properties of computer here
}
...或者如果您更喜欢单行版本:
(robot.childNodeWithName("computer") as? SKSpriteNode)?.size.height = 100
希望这对您有所帮助。