3

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sdd
  • 889
  • 8
  • 29

1 Answers1

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.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148