Joymaker Joymaker - 3 years ago 70
Swift Question

Why doesn't this SpriteKit program draw anything?

The associated ViewController is the auto-generated default, and calls presentScene on a GameScene object. I can demonstrate that the closure runs, but the SKLabelNode never appears. Black screen.

BTW, for reasons pertaining to a much more complex project, we want to learn how to create an SKScene programmatically, not using a Nib. This test project has no Nib.

import SpriteKit

class GameScene: SKScene {
var label : SKLabelNode?
var mouseLife = 30

override func didMove(to view: SKView) {
label = SKLabelNode(text: "30")
label?.alpha = 1.0
label?.position = CGPoint(x: size.width / 2, y: size.height / 2)
label?.fontSize = 10
label?.color =

let wait = SKAction.wait(forDuration: 1)
let tick = {
self.mouseLife = self.mouseLife - 1
self.label!.text = String(self.mouseLife)
print("tick action ran")
let action = SKAction.sequence([wait, tick])
let forever = SKAction.repeatForever(action)

Answer Source

Thanks, Fluidity, your comment was helpful to point me in the right direction, and cause the right experiments to be done.

In fact, the error was even worse than that: my GameScene instance was not being given a size on initialization, so it defaulted to a size of zero. In practice this meant the entire display was one logical pixel, which was either white or black depending on whether text happened to cover the (0, 0) point.

Once that problem was solved, we were quickly able to organize around an appropriate anchor point.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download