0

I don't know if it is a problem started in Swift 1.2 or not. I am new to both swift and SpriteKit. I was watching an online tutorial and the guy there was able to put a green box on the bottom-left corner of the screen by doing the following:

let greenBox = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 200, height: 200))
let somePoint = CGPointMake(0, 0)
greenBox.position = somePoint
self.addChild(greenBox)

However when I try the same thing, it does not even appear on the screen! Later, I found out that bottom left of the screen was actually something close to (300,10). Why would that happen?

Also, I found out that the self.frame.size equals to (1024.0, 768.0) which is even more confusing since it has no relation iPhone6's size. (I was testing with iPhone 6 though.)

I am stuck at this. Any help will be appreciated, thanks!

sangony
  • 11,636
  • 4
  • 39
  • 55
  • You need to set the `scaleMode` of the scene to `.ResizeFill`. – ABakerSmith Jul 09 '15 at 11:34
  • possible duplicate of [Dealing with different iOS device resolutions in SpriteKit](http://stackoverflow.com/questions/25205882/dealing-with-different-ios-device-resolutions-in-spritekit) – Epic Byte Jul 09 '15 at 15:08

1 Answers1

1

There are a few things. First of all, make sure your gameViewController.swift has this code in the viewDidLoad function:

        self.screenSize = skView.frame.size.width

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill
        scene.size = skView.bounds.size

This will make sure you have the right aspect ratio and size of an iPhone screen.

To get the bottom left of the screen, you can use this:

        let somePoint = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame))

Finally, I believe the greenBox has a center anchor point, so it will put the center of the box in the bottom left which means that about 75% of the block will fall off screen.

You can change the anchor point to the bottom left of the green box to make sure it shows entirely.

    greenBox.anchorPoint = CGPointMake(0, 0)
Nick Groeneveld
  • 895
  • 6
  • 18
  • ViewController does not have screenSize property. And you have assigned that to width which you meant saying "skView.frame.size" I suppose. Rest of your answer helped a lot! Especially the anchorPoint. Feels like I have to read the docs before setting sail. – Ömer Sakarya Jul 09 '15 at 23:56