title: Confusion about coordinates, frames & child nodes in SpriteKit on iOS?
body: <p>I'm still playing around with learning SpriteKit in iOS &amp; have been doing lots of reading &amp; lots of experimenting.  I'm confused by something else I've found* regarding coordinates, frames &amp; child nodes.</p>
  
  <p>Consider this snippet of code, in which I'm trying to draw a green box around my spaceship sprite for debugging purposes:</p>
  
  <pre><code>func addSpaceship()
  {
      let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
      spaceship.name = "spaceship"
  
      // VERSION 1
      spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
  
      let debugFrame = SKShapeNode(rect: spaceship.frame)
  
      debugFrame.strokeColor = SKColor.greenColor()
      spaceship.addChild(debugFrame)
  
      // VERSION 2
     //       spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
  
      spaceship.setScale(0.50)
  
  
      self.addChild(spaceship)
  }
  </code></pre>
  
  <p>If I add the set the spaceship sprite with the line marked "VERSION 1" above, I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/XsYn9.png" alt="Version 1"></p>
  
  <p>which is clearly wrong.  But if I comment <em>out</em> the line marked "VERSION 1" above, and instead use the line marked "VERSION 2", I get what I want:</p>
  
  <p><img src="https://i.stack.imgur.com/iAr6C.png" alt="Version 2"></p>
  
  <p>Notice that the actual code for the lines marked Version 1 &amp; Version 2 is identical:
  spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))</p>
  
  <p>So why does it matter when I set the position of the spaceship sprite?</p>
  
  <p>To my way of thinking, the position of the spaceship sprite is irrelevant to the placement of the the debugFrame, because the debugFrame is a child of the spaceship sprite &amp; thus, it's coordinates should be relative to the spaceship sprite's frame - right?</p>
  
  <p>Thanks
  WM</p>
  
  <p>*This is somewhat related to a question I asked yesterday:</p>
  
  <p><a href="https://stackoverflow.com/questions/27701705/in-spritekit-on-ios-scaling-a-textured-sprite-produces-an-incorrect-frame">In SpriteKit on iOS, scaling a textured sprite produces an incorrect frame?</a></p>
  
  <p>but a) I understand that one now, and b) this is a different enough that it deserves its own question.</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Hmmm - thanks, guys for the ideas below, but I still don't get it &amp; maybe this will help.</p>
  
  <p>I modified my code to print out the relavant positions &amp; frames:</p>
  
  <pre><code>func addSpaceship()
  {
      let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
      spaceship.name = "spaceship"
      println("Spaceship0 Pos \(spaceship.position) Frame = \(spaceship.frame)")
  
      // VERSION 1 (WRONG)
      spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
      println("Spaceship1 Pos \(spaceship.position) Frame = \(spaceship.frame)")
  
      let debugFrame = SKShapeNode(rect: spaceship.frame)
      println("DEBUG Pos \(debugFrame.position) Frame = \(debugFrame.frame)")
  
      debugFrame.strokeColor = SKColor.greenColor()
      spaceship.addChild(debugFrame)
  
      // VERSION 2 (RIGHT)
  //      spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
  //      println("Spaceship2 Pos \(spaceship.position) Frame = \(spaceship.frame)")
  
      spaceship.setScale(0.50)
  
  
      self.addChild(spaceship)
  }
  </code></pre>
  
  <p>Then, I ran it both ways got these results.  Since I understand Version 2, let's start there.</p>
  
  <p>Running with the "VERSION 2 (RIGHT)" code, I got:</p>
  
  <pre><code>Spaceship0 Pos (0.0,0.0) Frame = (-159.0,-300.0,318.0,600.0)
  DEBUG Pos (0.0,0.0) Frame = (-159.5,-300.5,319.0,601.0)
  Spaceship2 Pos (384.0,512.0) Frame = (225.0,212.0,318.0,600.0)
  </code></pre>
  
  <p>The spaceship node starts, by default, with its position at the bottom left of the screen (Spaceship0). Its frame is also expressed in terms of its anchor point (center) being set in the bottom left of the screen, hence the negative numbers for the origin of its frame rect.</p>
  
  <p>The debug frame is then created with its position set to 0, 0 by default &amp; its frame set to be the same as the spaceship's.</p>
  
  <p>The code (Spaceship2) then moves the spaceship node to a position in the view's coordinates (384.0,512.0), and its frame's origin is moved by adding the new position to the old origin (i.e. 384 + -159 = 225).</p>
  
  <p>All is well.</p>
  
  <p>Unfortunately, I still don't get Version 1.</p>
  
  <p>When I run with the "VERSION 1 (WRONG)," code, I get</p>
  
  <pre><code>Spaceship0 Pos (0.0,0.0) Frame = (-159.0,-300.0,318.0,600.0)
  Spaceship1 Pos (384.0,512.0) Frame = (225.0,212.0,318.0,600.0)
  DEBUG Pos (0.0,0.0) Frame = (0.0,0.0,543.5,812.5)
  </code></pre>
  
  <p>As above, the spaceship node starts, by default, with its position at the bottom left of the screen (Spaceship0). Its frame is also expressed in terms of its anchor point (center) being set in the bottom left of the screen, hence the negative numbers for the origin of its frame rect.</p>
  
  <p>The code (Spaceship1) then moves the spaceship node to a position in the view's coordinates (384.0,512.0), and its frame's origin is moved by adding the new position to the old origin (i.e. 384 + -159 = 225).</p>
  
  <p>The debug frame is then created with its position set to 0, 0 by default &amp; its frame set to have a strange width (543.5) &amp; a strange height (812.5).   Since I'm initializing the debugFrame.frame with spaceship.frame (i <em>think</em> that's what the default initializer does), I would expect the new debugFrame.frame to be the same as the spaceship's frame - but it isn't! The debug frame width &amp; height values apparently come from adding the actual width &amp; height to the origin of the spaceship node frame (543.5 = 225 + 318.5).  But if that is the case, why is t's frame rect origin still 0, 0 &amp; not the same adding (225.0 + 0 = 225.0)???</p>
  
  <p>I don't get it.</p>