Search code examples
sprite-kitsknode

Spritekit endless runner parent SKNode


I'm familiar with swift but I've started dabbling my hand in spritekit. I've been following a tutorial about created an endless runner. The approach taken by the author is to create a single SKNode that contains all the children. This container node is then moved rather than moving all the child Nodes individually. But what's got me stumped is that the container node doesn't have a size associated with it so I'm a bit confused as to how/why this works and the author doesn't really explain it.

So we have

    let containerNode = SKNode()
    let thePlayer = Player("image":"player") //SKSpriteNode
    let inc = 0
    override func didMoveToView(){
        self.anchorPoint = CGPointMake(0.5,0)
        addChild(containerNode )
       containerNode.addChild(player)
         moveWorld()
    }

   func moveWorld(){
      let moveWorldAction = SKAction.moveByX(-screenWidth, y:0, duration:6)
     let block = SKAction.runBlock(movedWorld)
      let seq = SKAction.sequence([moveWorldAction,block])
      let repeatAction = SKAction.repeatActionForever(seq)
      containerNode.runAction(repeatAction)
   }

   func movedWorld() {
     inc = inc + 1
     addObjects()

   }

   func addObjects() {
   let obj = Object()
   containerNode.addChild(obj)
   let ranX = arc4random_uniform(screenWidth)
   let ranY = arc4random_uniform(screenHeight)
   obj.position = CGPointMake(screenWidth * (inc + 1) + ranX, ranY) 
   }

There's some conversion that I've omitted in the code above from int to float but it's not necessary for the point I want to understand.

I get why when new objects are created we do the multiple by the increment, but what I don't get is the containerNode doesn't have a size, so why do it's children show? Is this the most efficient way to do this?

I'm assuming that it's just convenience rather than moving all the other objects individually but the fact that it doesn't have a size is confusing me.


Solution

  • If I understand the documentation for SKNode correctly this little section should answer your question:

    Every node in a node tree provides a coordinate system to its children. After a child is added to the node tree, it is positioned inside its parent’s coordinate system by setting its position properties. A node’s coordinate system can be scaled and rotated by changing its xScale, yScale, and zRotation properties. When a node’s coordinate system is scaled or rotated, this transformation is applied both to the node’s own content and to that of its descendants. Source

    Again, if I'm reading this correctly, it seems that when a node is added to a node tree it inherits it's parent coordinate system. So by default containerNode is the same size as self in this case.

    Also according to the frame property of a SKNode:

    The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Source

    Which again sounds like if a frame isn't set it takes the smallest rectangle (self in this case or it might be understood that it's the children of containerNode I'm not sure) as it's own frame.

    Whatever the case is, it's inheriting it from another SKNode.