3

This is sort of a stupid question, but what does the function self.addChild() do?

I am familiar with this function, and how to use it, but I am not exactly sure what it adds the child to.

For instance, I have created and designed an SKShapeNode called spinnyNode. Then I call the function:

func touchDown(atPoint pos : CGPoint) {
    if let n = self.spinnyNode?.copy() as! SKShapeNode? {
        n.position = pos
        n.strokeColor = SKColor.black
        self.addChild(n)
    }

What is the parent in this situation? Is it the view that the node is being created in?

Thank you so much for your time, and your answering of stupid questions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JustAnotherGuy
  • 259
  • 3
  • 14
  • The parent is essentially the view, and what you are doing is adding that node to the view. Without this call, the node would be created, but never passed to the view, so it wouldn't spawn. Think of it as doing some homework for a teacher, then the addChild submits it. If you don't submit it, it isn't used. So whatever class your function is in, be it ViewController etc, that is the parent the node is being passed to. And don't worry, it isn't a stupid question, learning the foundations of how these functions work is a great way to build better apps! – Danny Wilson Nov 24 '16 at 00:57
  • Thank you for your time, that is very good to know. – JustAnotherGuy Nov 24 '16 at 01:00
  • @DannyWilson Actually, it adds it to the scene. That's a major difference between `self.addChild()` and `self.view.addSubview()`. And it gets added to the SKScene, not View Controller – Nik Nov 24 '16 at 01:10
  • @Nik Ah yes, sorry, I have revised my answer – Danny Wilson Nov 24 '16 at 01:14

2 Answers2

2

The parent is essentially the scene, and what you are doing is adding that node to the scene. Without this call, the node would be created, but never passed to the scene, so it wouldn't spawn.

Think of it as doing some homework for a teacher, then the addChild submits it. If you don't submit it, it isn't used. So whatever class your function is in, be it SKScene etc, that is the parent the node is being passed to.

And don't worry, it isn't a stupid question, learning the foundations of how these functions work is a great way to build better apps!

Danny Wilson
  • 331
  • 2
  • 11
2

Lets break it down:

self in this case, refers to the SKScene which your are currently in. So if you're in your gameScene, the node will be added to the gameScene. Note that you can add nodes to other nodes, so if you have an SKNode named gameLayer, you could add a node to gameLayer, which would then be added to the scene. (That would look like this: gameLayer.addChild(node)) If there is no specified location for the node, it default chooses self

addChild(node) is the function that actually adds the specified node to the specified location (see above). You tell the compiler which node to add to the scene by putting its declared name in the brackets.

Make sure you're setting the nodes attributes (position, size, etc...), as they stay the default values until you change them.

Nik
  • 1,664
  • 2
  • 14
  • 27