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)
}
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With