1

I just started a project and add the following lines of code:

override func didMoveToView(view: SKView) {
    let myNode = SKNode()
    myNode.position = CGPoint(x: 250.0, y: 300.0)
    addChild(myNode)

    let mySprite = SKSpriteNode(imageNamed: "Spaceship")
    mySprite.position = CGPoint(x: 0, y: 0)
    mySprite.zPosition = 1
    myNode.addChild(mySprite)

    let myOtherSprite = SKSpriteNode(imageNamed: "Spaceship")
    myOtherSprite.position = CGPoint(x: 50.0, y: 50.0)
    myOtherSprite.zPosition = 2
    myNode.addChild(myOtherSprite)
}

but the sprites don't appear in the scene. What's the problem here? I checked my code several times, and I think there are no errors.

0x141E
  • 12,613
  • 2
  • 41
  • 54
  • I started a new SpriteKit template and replace the default `didMoveToView` with your method and the spaceship appeared. BTW, a typo in the definition of the `myOtherSprite`, should be "Spaceship". Do you run on a simulator or a physical device? – WangYudong Dec 20 '15 at 09:52
  • @Orkhan Probably your scene and a view have different size / proportion. http://stackoverflow.com/a/30835844 Hint: read about scene's scale modes. – Whirlwind Dec 20 '15 at 09:58

1 Answers1

0

Add scene.size = skView.bounds.size to your GameViewController's viewDidLoad method so that it looks like this:

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Fix Size */
        scene.size = skView.bounds.size

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

As Knight0fDragon noted below, this will affect how autoLayout works, so it may not work for all solutions.

Trent Sartain
  • 446
  • 2
  • 10
  • This will get jacked up with Autolayouts, if everything is default in the project, the scene size will always be 600x600 do to autoconstraints not taking place, which I believe is the same scene size by default, but not 100% sure on that and do not have XCode on right now to test – Knight0fDragon Dec 21 '15 at 18:45