Skoua
Skoua

Reputation: 3603

Swift & iOS8 : SKLabelNode not showing up

So I'm trying to learn Swift and Sprite kit and I started following this Apple tutorial (you might need to login to Apple Developer): https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/GettingStarted/GettingStarted.html#//apple_ref/doc/uid/TP40013043-CH2-SW1

I translated the code given from Obj-C to Swift and it works quite well BUT my SKLabelNode named helloNode is not showing up despite the fact that Sprite kit is telling me that there is indeed 1 node on scene.

Here's the code:

class HelloScene: SKScene {
var contentCreated = false

override func didMoveToView(view: SKView) {
    if(!self.contentCreated) {
        createSceneContents()
        contentCreated = true
    }
}

func createSceneContents() {
    self.backgroundColor = SKColor.blueColor()
    self.scaleMode = SKSceneScaleMode.AspectFit
    self.addChild(newHelloNode())
}

func newHelloNode() -> SKLabelNode {
    let helloNode = SKLabelNode(fontNamed: "Chalkduster")
    helloNode.text = "Hello, World!"
    helloNode.fontSize = 42
    helloNode.fontColor = SKColor.whiteColor()
    helloNode.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
    return helloNode
}

}

HelloScene is instantiated and successfully added to the main View Controller.

Thanks!

Upvotes: 1

Views: 1081

Answers (1)

hswets
hswets

Reputation: 61

I had the same issue. The issue is with auto layout in xcode 6. The scene is being loaded before the auto layout settings are set. In the ViewController place the initialization of the scene and presentScene in viewDidLayoutSubviews like so:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews();

    let hello = HelloScene.new();
    hello.size = skView.bounds.size;
    hello.scaleMode = SKSceneScaleMode.AspectFill;

    skView.presentScene(hello);
}

Upvotes: 2

Related Questions