1

I have this inside my GameScene which is called in the didMove()

for i in 1...5 {

    // path to create the circle
    let path = UIBezierPath(arcCenter: CGPoint(x: center.x, y: center.y), radius: CGFloat(((43 * i) + 140)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)

    // the inside edge of the circle used for creating its physics body
    let innerPath = UIBezierPath(arcCenter: CGPoint(x: center.x, y: center.y), radius: CGFloat(((43 * i) + 130)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)

    // create a shape from the path and customize it
    let shape = SKShapeNode(path: path.cgPath)
    shape.lineWidth = 20
    shape.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)

    // create a texture and apply it to the sprite
    let trackViewTexture = self.view!.texture(from: shape)
    let trackViewSprite = SKSpriteNode(texture: trackViewTexture)
    trackViewSprite.physicsBody = SKPhysicsBody(edgeChainFrom: innerPath.cgPath)
    self.addChild(trackViewSprite)

}

It uses UIBezierPaths to make a few circles. It converts the path into a SKShapeNode then a SKTexture and then applies it to the final SKSpriteNode.

When I do this, the SKSpriteNode is not where it should be, it is a few to the right:

enter image description here

But when I add the SKShapeNode I created, it is set perfectly fine to where it should be:

enter image description here

Even doing this does not center it!

trackViewSprite.position = CGPoint(x: 0, y: 0)

No matter what I try it just will not center.

Why is this happening? Some sort of bug when converting to a texture?

P.S - This has something to do with this also Keep relative positions of SKSpriteNode from SKShapeNode from CGPath But there is also no response :(

Edit, When I run this:

let testSprite = SKSpriteNode(color: UIColor.yellow, size: trackViewSprite.size)
self.addChild(testSprite)

It shows it has the same frame also: enter image description here

Community
  • 1
  • 1
ngngngn
  • 579
  • 4
  • 18
  • My first guess would be the frame size changes, when converting from shape to texture, you might be grabbing more than the size you think you are grabbing – Knight0fDragon Oct 13 '16 at 22:47
  • @Knight0fDragon I should print the frame and see if it has the same size? – ngngngn Oct 13 '16 at 23:08
  • just make the background white when you show it as a sprite, you should see more black than you expect – Knight0fDragon Oct 13 '16 at 23:09
  • 1
    oh... put a black square on your SKShapeNode that fills the screen – Knight0fDragon Oct 13 '16 at 23:19
  • @Knight0fDragon, I am not able to set the background color of the texture, shape, or sprite. Maybe I misunderstood what you said? And what do you mean a black sqaure, like using the 'shape.frame' and create another shapenode with that frame to see? Thanks by the way, this problem has been really been bothering me – ngngngn Oct 13 '16 at 23:36
  • 1
    lol sorry, I am tired, what I told you wouldn't work, Just add a yellow SKSpriteNode the size of the texture to your sprite, not shape. `let test = SKSpriteNode(color:SKColor.yellow, size:sprite.size);spr.zPosition = -1; sprite.addChild(spr);` – Knight0fDragon Oct 13 '16 at 23:46
  • Oh I totally missed that constructor. Alright, so from that is this: http://imgur.com/a/mMJE5. Seems like the frame size has the right width? – ngngngn Oct 13 '16 at 23:53
  • definitely not, look at all that extra yellow on the side – Knight0fDragon Oct 14 '16 at 00:12
  • you can specify the size of the texture you want to grab from the view with let trackViewTexture = self.view!.texture(from: shape, crop:rect) https://developer.apple.com/reference/spritekit/skview/ – Knight0fDragon Oct 14 '16 at 00:17
  • Yeah, I tried that along with a ton of other stuff. Still same issue though, here is what I have now: `let trackViewTexture = self.view!.texture(from: shape, crop: shape.frame)! let trackViewSprite = SKSpriteNode(texture: trackViewTexture) self.addChild(trackViewSprite)` – ngngngn Oct 14 '16 at 00:25
  • you can't use the shapes frame, it is calculating wrong, you know the color sprite we made? do that to the skskape just to see what its frame looks like. You can't use the texture though, because it adds to the frame – Knight0fDragon Oct 14 '16 at 00:26
  • Oh ok so I did what we did to the sprite but to the shape, `let testSprite = SKSpriteNode(color: UIColor.yellow, size: shape.frame.size)` and it came out like this, http://i.imgur.com/rIxesug.png. So if I can't use the shape's frame, should I calculate it manually then? – ngngngn Oct 14 '16 at 00:34
  • why is it calculating so big – Knight0fDragon Oct 14 '16 at 00:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125652/discussion-between-knight0fdragon-and-nicholas714). – Knight0fDragon Oct 14 '16 at 00:36

1 Answers1

2

After a long discussion, we determined that the problem is due to the frame size not being the expected size of the shape.

To combat this, the OP created an outer path of his original path, and calculated the frame that would surround this. Now this approach may not work for everybody.

If anybody else comes across this issue, they will need to do these things:

1) Check the frame of the SKShapeNode to make sure that it is correct
2) Determine what method is best to calculate the correct desired frame
3) Use this new frame when getting textureFromNode to extract only the desired texture size

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44