我有一个SKScene的子类,我想在其中从.sks
文件中加载场景。我必须使用super.init(size: CGSize)
而不是super.init(fileNamed: String)
来初始化我的子类的初始化中的SKScene,因为super.init(fileNamed: String)
不是指定的初始化程序。如何子类化SKScene并使用super.init(fileNamed: String)
的语义从文件加载场景?
import SpriteKit
class Spielfeld: SKScene {
var x: Int // Anzahl Felder in x-Richtung
var y: Int // Anzahl Felder in y-Richtung
var up: SKSpriteNode = SKSpriteNode()
var down: SKSpriteNode = SKSpriteNode()
var left: SKSpriteNode = SKSpriteNode()
var right: SKSpriteNode = SKSpriteNode()
init(_ x: Int, _ y: Int, fileNamed: String, size: CGSize) {
self.x = x
self.y = y
super.init(size: size)
up = self.childNode(withName: "Up") as! SKSpriteNode
down = self.childNode(withName: "Down") as! SKSpriteNode
left = self.childNode(withName: "Left") as! SKSpriteNode
right = self.childNode(withName: "Right") as! SKSpriteNode
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
答案 0 :(得分:0)
没有必要,因为.sks中有一个自定义类。您只需将“ Spielfeld”放入其中。
然后在viewController中:
if let sceneNode = SKScene.init(fileNamed: "Spielfeld(Whatever your name is here)") {
(sceneNode as! Spielfeld).property = "anyValueToUse"
if let view = self.view as! SKView? {
view.presentScene(sceneNode) //present the scene.
}
如果您决定从文件加载,则没有理由再次初始化。您可以通过覆盖以下内容来添加init函数:
override func sceneDidLoad() {
} //called after .sks is loaded.
override func didMove(to view: SKView) {
//change the size here.
} // called after presentation.