1

I'm trying to draw a rectangle around my screen. Here's my code:

    let viewRect = SKShapeNode(rect: self.view!.frame)
    viewRect.strokeColor = SKColor.whiteColor()
    viewRect.lineWidth = 2.0
    addChild(viewRect)

I'm using self.view!.frame instead of self.frame because my scene is not the same size as my screen (the scene is stretched to fill my screen).

I would have expected the code to draw a rectangle around my screen, but it draws this instead (the white rectangle is what is drawn):

Anyone know why it's not being drawn around my screen?

Scene size: 1024x768 View size: 414x736 Scale mode: Aspect Fill

dfgdfg
  • 85
  • 6
  • Please read all this to see if something applies to your situation : http://stackoverflow.com/a/30714859/3402095 In the meanwhile you can update your question with some debugging information like - what is your real scene size, what is your view size and what is your scene' scale mode... – Whirlwind Jun 13 '15 at 15:45
  • It is a bit difficult to tell as your snippet of code is out of context - but it may be your use of 'frame' (the position of the view in its superview) rather than 'bounds' (the position and size of the view itself) to set the size of the rectangle. – Ali Beadle Jun 13 '15 at 15:57
  • @AliBeadle Unfortunately, neither of them seem to work. – dfgdfg Jun 13 '15 at 16:00
  • @Whirlwind Unfortunately the solution in the link didn't help. I added in the debugging info you asked for though! (its at the bottom) – dfgdfg Jun 13 '15 at 16:04
  • @dfgdfg What is self in this context - presumably the SKView that you want the border around? In which case I am not sure why you are using self.view!.frame rather than self.bounds. – Ali Beadle Jun 13 '15 at 16:08
  • @AliBeadle Self is a SKScene. The code is being written in "GameScene.swift". – dfgdfg Jun 13 '15 at 16:11
  • @dfgdfg Have you read the comments as well? 1024x768 is default size of a scene when loaded from .sks file. Are you sure that is the size you want? – Whirlwind Jun 13 '15 at 16:44
  • I think that what you looking for is GameScene *scene = [GameScene sceneWithSize:skView.bounds.size]; but maybe I don't understand completely what you are trying to achieve, and why do you have a scene of 1024x768 and a view with a totally different ratio. – Whirlwind Jun 13 '15 at 16:52
  • @Whirlwind I definitely need the scene to be 1024x768. It's hard to explain why, but the current dimensions are far more suitable for my game, despite the fact that they aren't the same size as my view. – dfgdfg Jun 13 '15 at 16:58

1 Answers1

0

You are creating your border as a node within the scene but then setting its coordinates according to the view that is presenting the scene. The SKScene's coordinate system is not generally the same as the view presenting it so that is why the shape is different to what you expect.

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
  • Thanks! How would I fix this? – dfgdfg Jun 13 '15 at 17:22
  • That depends on what you are trying to acheive. At its simplest you could just use your known scene size - 1024,768, but as Whirlwind says leaving your scene at that default size is probably not what you want and anyway with Aspect Fill part of your scene is probably off screen anyway. Have you read and understood Apples notes on this subject: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html – Ali Beadle Jun 13 '15 at 17:28