Reputation: 55
I wanted to create a game in Xcode 7.2. I wanted to create a info screen which will have some labels inside. But nothing shows up on the newly created scene. Whatever I put on the new scene, like images, sprites, labels. Nothing appears on the screen when I run it. But the code I wrote to create the label works perfectly fine in the "GameScene" which the game initially contains.
The code that I put in the InfoScene is the following:
import SpriteKit
class InfoScene: SKScene {
override func didMoveToView(view: SKView) {
let thanksLabel = SKLabelNode(fontNamed:"Arial")
thanksLabel.text = "Thank you for Playing!"
thanksLabel.fontSize = 45
thanksLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(thanksLabel)
}
}
Nothing seems wrong to me. If I put the above code in the GameScene which the template originally has, them the label will appear. Also, if I add a sprite on the scene, it doesn't show up as well. This really confuses me.
Upvotes: 4
Views: 236
Reputation: 35372
I think you've simply forgot to put your code to an instance method like:
Swift 2.x:
override func didMoveToView(view: SKView) {
// put your stuff here
}
Swift 3.x:
override func didMove(to view: SKView){
// put your stuff here
}
didMove
is called immediately after a scene is presented by a view. According to the actual documentation you might use this method to create the scene’s contents.
Update: (after your corrections and comment):
I think InfoScene
is not launched for some reason.
Make sure your InfoScene
is launched using breakpoints inside didMoveToView
or simply add this line between your lines:
print("∙ \(type(of: self))")
Upvotes: 3
Reputation: 16827
Are you sure you are using your InfoScene
class? How are you making this instance? Through an SKS file? if so, you need to change the Custom class field to point to your InfoScene
, by default it will go to SKScene
Upvotes: 1