Search code examples
iosswiftparent-childchild-nodes

Change size of a childnode in Swift


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

 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?

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.


Solution

  • 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.