user2453229
user2453229

Reputation: 11

Why won't my SKSpriteNodes appear in the scene?

I am trying to add a couple of nodes to the scene. When I run the app I just have the default gray background. Neither of the nodes appear in the scene. Here is what I have:

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

        let jumper = SKSpriteNode(imageNamed:"Jumper")
        jumper.xScale = 0.25
        jumper.yScale = 0.25
        jumper.position = CGPointMake(50, 300)

        self.addChild(jumper)

        let blob = SKSpriteNode(imageNamed:"Blob")
        blob.position = CGPointMake(160, 30)
        blob.xScale = 0.5
        blob.yScale = 0.25

        self.addChild(blob)
    }

I have checked the image titles and everything is correct there.

Upvotes: 1

Views: 4573

Answers (1)

0x141E
0x141E

Reputation: 12773

The Problem

Your sprites are not visible because your scene is not completely within the simulator's window. By default the scene's size is 1024 x 768 (specified by GameScene.sks), while the simulator is displaying only 375 x 667 points (if you selected the iPhone 6/6s simulator). Unless the scene's size is set correctly, the bottom-left corner of the simulator's window will not be the (0,0) position of your scene.

To correct this issue,

Method 1:

Change the scene's size property in GameScene.sks to match the size of the view. I suggest you set the scene size to 375 x 667 (iPhone 6/6s). If you do this and set the scene's scaleMode property to .AspectFill (the default value), SpriteKit will automatically scale the scene to fit in the view of the iPhone 6+, 6s+, 5, and 5s. This will save you time when porting your game to these devices.

Method 2: In your view controller, replace this

scene.scaleMode = .AspectFill

with this

scene.scaleMode = .ResizeFill

Warning: Using this method will work but may require additional effort to port your game to other iOS devices with a different screen size.

Upvotes: 12

Related Questions