Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift & SpriteKit: Full screen image

I'm making a first scene with SpriteKit in swift. I created a background image for 4 inch iPhone with dimensions 640x1136. I placed the image as sprite in centre of the screen, but the image is not full screen, there are edges missing.

Next, I tried to resize the image within app. When I did image.size.height = self.size.height the height got properly resized along full iPhone screen. But when I did the same with width image.size.width = self.size.width then the picture got extremely stretched width wise.

I printed the dimensions of self.size and it turns out the dimension for my iPhone 5s are 1024, 768. This is total nonsense as the screen can't be wider than its height on iPhone. The app is universal but the only orientation is set to portrait.

Edit: This is the code I use to put the image on the screen

class GameScene: SKScene
{
    let menuImage = SKSpriteNode(imageNamed: "menuImage")

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

        menuImage.anchorPoint = CGPointMake(0.5, 0.5)
        menuImage.size.height = self.size.height
        menuImage.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

        self.addChild(menuImage)
    }
like image 560
Sam Avatar asked Aug 27 '14 13:08

Sam


2 Answers

You need to add

scene.size = skView.bounds.size

or for Swift 5

scene.size = self.view.bounds.size

just before skView.presentScene(scene) in your gameViewController file

like image 75
Clément Bisaillon Avatar answered Oct 09 '22 04:10

Clément Bisaillon


This is what worked for me to stretch the background image to fit the whole screen in Swift 5.

background.size = CGSize (width: frame.maxX, height: frame.maxY)
like image 33
Brian M Avatar answered Oct 09 '22 05:10

Brian M