I'm very new to iOS development, but I've been playing around with the SpriteKit template app to learn how things work and try to boot up on Swift while I'm at it. One thing I'm having trouble with is how to work with SpriteKit subclasses.
I'm in the GameScene.swift file and I'm trying to extract a class for the "Hello World" label, so here's what that file looks like:
// GameScene.swift
import SpriteKit
class HelloLabel: SKLabelNode {
init(fontNamed: String) {
super.init(fontNamed: fontNamed)
self.text = "Hello, World!"
self.fontSize = 65;
self.position = CGPoint(x: 400, y: 500);
}
}
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// let myLabel = SKLabelNode(fontNamed:"Chalkduster")
// myLabel.text = "Hello, World!";
// myLabel.fontSize = 65;
// myLabel.position = CGPoint(x: 400, y: 500);
let myLabel = HelloLabel(fontNamed: "Chalkduster")
self.addChild(myLabel)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* snip, no changes made here */
}
override func update(currentTime: CFTimeInterval) {
/* snip, no changes made here */
}
}
So, HelloLabel
is intended to just be a pass-through in an attempt to understand how everything wires together, but when I run the app, I get the following error:
/Users/jon/Projects/ErrorExample/ErrorExample/GameScene.swift: 11: 11: fatal error: use of unimplemented initializer 'init()' for class 'ErrorExample.HelloLabel'
I'm not understanding what this message is trying to tell me. The way I read this error is that its complaining that I haven't implemented an initializer called init
in the class ErrorExample.HelloLabel
, but it sure looks like I have to me!
So, what am I doing wrong here - how does one extract a class to hide all this setup?