4

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

Community
  • 1
  • 1
José María
  • 2,835
  • 5
  • 27
  • 42
  • If images are at half size on a retina screen, then you probably aren't labeling them as 2x, but if your image is only 640 by 1136 then it will be half the size of a retina screen since it has twice the pixels. Make sure you are distinguishing between points and pixels. – Cooper Buckingham Jul 12 '14 at 18:17
  • @CHBuckingham The images are correctly placed at the xcassets folder. Also, points are in non-retina resolution, no? – José María Jul 12 '14 at 19:23
  • I commented too quickly and assumed you were running on an iPad simulator resolution. A 640 by 1136 pixels image should fit the iPhone retina screen display correctly. So your source image either isn't that density or resolution, or your scene has a different size or scale. – Cooper Buckingham Jul 12 '14 at 20:09
  • @CHBuckingham How do I check or change the Scene size or scale? – José María Jul 12 '14 at 20:48
  • The scene should have a property for xScale and yScale, and a self.size.width and self.size.height. – Cooper Buckingham Jul 12 '14 at 23:13
  • What is a value of `backgroundName`? Make sure it doesn't contain file extension at the end of the file name – Andrey Gordeev Jul 13 '14 at 04:14
  • @AndreyGordeev the value of backgroundName is randomly chosen using a switch statement and arc4random_uniform. And no, it doesn't have the extension. – José María Jul 13 '14 at 09:42
  • @CHBuckingham OK, but if you don't specify them in the code, they have default values, no? – José María Jul 13 '14 at 09:42
  • @CHBuckingham Well, I've found the problem. I have the game as a universal binary (iPad and iPhone). The size of the scene is 1024x768, the size of the iPad display. That's why the image doesn't display correctly. How can I adjust the size so it has the correct resolution on the iPhone 3.5" and 4" displays? – José María Jul 13 '14 at 09:46

3 Answers3

1

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 )
    }
}
Linda MacPhee-Cobb
  • 7,646
  • 3
  • 20
  • 18
1

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
rookr
  • 193
  • 2
  • 7
0

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.

dylanrw
  • 1
  • 3
  • Hmm, nope it does not seem to b scaled as far at that I can look in the code. Also, the image is correctly stored in the xcassets folder so I don't know what can be the problem – José María Jul 12 '14 at 17:34