在Swift中更改子节点的大小

时间:2021-08-01 23:49:27

I have 2 SKSpritenodes: robot & computer robot is the parentNode and computer is the childNode

我有两个SKSpritenodes:机器人和计算机机器人是父节点,计算机是子节点

 robot.addChild(computer)

Now I want to change the size of the computer by just using the name of the parentNode. So I need a code like: robot.childnode.size.width = xxx How can I do this?

现在我想通过使用parentNode的名称来更改计算机的大小。所以我需要一个代码:robot.childnode.size。宽度= xxx我该怎么做?

the reason for this: I have multiple skspritenodes called robot and I can detect them with collision which one it is, so I need this code to acces the childnode of that particular parentNode.

这样做的原因是:我有多个skspritenodes称为robot,我可以通过冲突检测它们,就是这个,所以我需要这个代码来访问那个特定的parentNode的子节点。

1 个解决方案

#1


1  

Set a name for the computer node when you are adding it to robot.

在将计算机节点添加到机器人时为其设置一个名称。

computer.name = "computer"
robot.addChild(computer)

Later on you can write...

以后你可以写……

if let computer = robot.childNodeWithName("computer") as? SKSpriteNode {
    // you can install El Capitain and change the properties of computer here
}

... or if you prefer the single line version:

…或者如果你喜欢单行版本:

(robot.childNodeWithName("computer") as? SKSpriteNode)?.size.height = 100

Hope this helps.

希望这个有帮助。

#1


1  

Set a name for the computer node when you are adding it to robot.

在将计算机节点添加到机器人时为其设置一个名称。

computer.name = "computer"
robot.addChild(computer)

Later on you can write...

以后你可以写……

if let computer = robot.childNodeWithName("computer") as? SKSpriteNode {
    // you can install El Capitain and change the properties of computer here
}

... or if you prefer the single line version:

…或者如果你喜欢单行版本:

(robot.childNodeWithName("computer") as? SKSpriteNode)?.size.height = 100

Hope this helps.

希望这个有帮助。