The Human Bagel
The Human Bagel

Reputation: 7224

SpritKit background image has margins

I am following a tutorial on SpriteKit and when I add my background image to the scene, the image does not fill up the entire scene (has margins).

The project is a universal project, and I am using size classes (same effect when I turn it off).

I am new to Swift and SpriteKit, so maybe I am missing something. Here is my code (from a brand new project):

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.anchorPoint = CGPointMake(0.5, 0.5)

    let background = SKSpriteNode(imageNamed: "Background")
    background.position = CGPointMake(0, 0)
    background.size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)

    self.addChild(background)

}

And here is a screenshot:

enter image description here

I added the position and size code just to be sure. Also, frame.size is showing (1024, 768) but bounds.size shows the proper (640, 1136). Any help will be greatly appreciated!

Upvotes: 0

Views: 352

Answers (1)

Jack Chorley
Jack Chorley

Reputation: 2344

You will find that in your ViewController.swift, the current scale mode is .AspectFill which is sometimes perfectly find to use but in your case, you need to change that to

scene.scaleMode = .ResizeFill

which will make sure that the size fits perfectly into any size screen. You then use self.view.frame.size instead of self.view.bounds.size and you are good to go! Ask if you need any more information, though the documentation explains the scaleModes quite well:

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKScene_Ref/index.html#//apple_ref/c/econst/SKSceneScaleModeFill

Upvotes: 1

Related Questions