我对iOS开发很陌生,但我一直在玩SpriteKit模板应用程序,以了解工作原理并尝试在
Swift上启动.我遇到的一件事就是如何使用SpriteKit子类.
我在GameScene.swift文件中,我正在尝试为“Hello World”标签提取一个类,所以这是该文件的样子:
// 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 */
}
}
因此,HelloLabel旨在成为一个传递,试图了解所有内容是如何连接在一起的,但是当我运行应用程序时,我收到以下错误:
/Users/jon/Projects/ErrorExample/ErrorExample/GameScene.swift: 11: 11: Fatal error: use of unimplemented initializer 'init()' for class 'ErrorExample.HelloLabel'
我不明白这条消息试图告诉我的是什么.我读这个错误的方式是它抱怨我没有在ErrorExample.HelloLabel类中实现一个名为init的初始化程序,但它确实看起来像我必须得到的!
那么,我在这里做错了什么 – 如何提取一个类来隐藏所有这些设置?
解决方法
我不确定为什么,但SKLabelNode中隐藏的功能试图调用没有参数的init函数.这似乎有效:
class HelloLabel: SKLabelNode {
init() {
super.init()
}
init(fontNamed fontName: String!) {
super.init(fontNamed: fontName)
self.text = "Hello,y: 500);
}
}