José María
José María

Reputation: 3053

Swift: SpriteKit not rendering sprites in correct size

EDIT 3: Well, I've fixed this problem. It seems that with iOS 8 you can now load the scene from a scene.sks file. So in this case, the game was loading a .sks file that had a resolution of 1024x768. That's why the image was not displayed correctly. Changing the ViewController to work as it was prior to iOS 8 (here it is explained: Swift and Spritekit won't run on device running iOS 7.1) seems to solve the issue. Thanks to everyone who tried to help me. :D

I'm creating a game in SpriteKit and Swift (well, I'm porting a game I have already made in Obj-C) using the latest Xcode beta. I have a problem and it is that SpriteKit does not seem to render my sprites correctly, rendering them in a smaller size than what it should be. If I provide a background in the native iPhone resolution, adding it using this code:

    let background = SKSpriteNode(imageNamed: backgroundName)
    background.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
    self.addChild(background)

Yields this:

SKFail

The sprite image is smaller than what it should be, as the image is 640x1136. Am I missing something?

EDIT: Well, it seems the images are rendered approximately 50% smaller. Making them a 50% bigger display them correctly but they have the wrong size (50% bigger)...

EDIT 2: Well, here's the proof that the images I'm using are at the correct resolution:enter image description here

Upvotes: 4

Views: 3308

Answers (3)

rookr
rookr

Reputation: 193

All you need to do to make it the same size is this (in didMoveToView):

self.size = view.frame.size // makes the size of the scene the same size as the frame, idk
let background = SKSpriteNode(imageNamed: "background") // if you actually need this tho
background.size = self.frame.size

Upvotes: 1

Linda MacPhee-Cobb
Linda MacPhee-Cobb

Reputation: 7856

I ran into the same problem. You must declare your texture once and only once, then load it in each sprite. If you println("sprite:\(sprite)") your sprites you'll see the texture is missing.

class test : SKSpriteNode
{
    let textureImage = SKTexture(imageNamed:"spriteImage")


    func load sprites() {

        sprite = SKSpriteNode(texture: textureImage )
    }
}

Upvotes: 1

dylanrw
dylanrw

Reputation: 1

Make sure that your scene isn't scaled or setup in some odd manner. My tests with the latest xcode/spritekit work with an image that size.

Upvotes: 0

Related Questions