我想在我的主GameScene上添加一个SKScene。 SKReferenceNode似乎是一个很好的解决方案。
我有: - GameScene.sks(主场景) - Countdown.sks(添加到GameScene的场景) - Countdown.swift(自定义类,如何初始化它?SKScene?SKReferenceNode?SKNode)
我不知道如何使用我的Countdown类以编程方式添加我的倒计时。
我试过了:
let path = Bundle.main.path(forResource: "Countdown", ofType: "sks")
let cd = SKReferenceNode (url: NSURL (fileURLWithPath: path!) as URL) as! Countdown
cd.name = "countdown"
self.addChild(cd)
但是我有以下错误:
Could not cast value of type 'SKReferenceNode' (0x10d97ad88) to 'LYT.Countdown' (0x10a5709d0
我也尝试过更简单的东西:
let cd=Countdown(scene:self)
self.addChild(cd)
但我不知道如何使用Countdown.sks文件初始化该类。
我知道我也有可能创建一个SKNode类,并以编程方式100%初始化它,但对我来说使用相关的.sks文件以使用Xcode场景编辑器非常重要。
我这样做,我不知道这是否是最好的方法,但有效:
我有2个文件Dragon.swift和sks
我添加了一个像DragonNode这样的“主”节点和其他节点子节点
现在,DragonNode是一个自定义类,在sks文件中设置它:
DragonNode是普通的SKSpriteNode
class DragonNode: SKSpriteNode, Fly, Fire {
var head: SKSpriteNode!
var body: SKSpriteNode!
var shadow: SKSpriteNode!
var dragonVelocity: CGFloat = 250
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//Example other node from sks file
body = self.childNodeWithName("Body") as! SKSpriteNode
head = body.childNodeWithName("Head") as! SKSpriteNode
shadow = self.childNodeWithName("Shadow") as! SKSpriteNode
shadow.name = "shadow"
}
//Dragon Func
func fireAction () {}
func flyAction () {}
}
在场景内,添加一个SKReferenceNode:
在SKScene代码中:
let dragonReference = self.childNodeWithName("DragonReference") as! SKReferenceNode
let dragonNode = dragonReference.getBasedChildNode() as! DragonNode
print(dragonNode)
//Now you can use the Dragon func
dragonNode.flyAction()
getBasedChildNode()
是查找基于节点的扩展(第一个)
extension SKReferenceNode {
func getBasedChildNode () -> SKNode? {
if let child = self.children.first?.children.first {return child}
else {return nil}
}
}
我对上面的Simone做了类似的事情,但是我没有扩展参考节点,而是将我的扩展添加到了SKNode。
extension SKNode {
func nodeReferenced() -> SKNode? {
if self .isKind(of: SKReferenceNode.self) {
return children.first!.children.first!
}
return nil
}
}
这样,如果节点实际上不是参考节点并且使这两步处理成为一个衬里,则不需要演员。我的版本会将以上代码更改为:
if let dragonNode = childNodeWithName("DragonReference")?.nodeReferenced() as? DragonNode {
print(dragonNode)
dragonNode.fly()
}
这对我有用,但是西蒙娜的回答似乎更直接,也许比我的灵活,所以我给他们分数。我只是喜欢干净的代码,因为我们几乎从未真正需要SKReferenceNode,我们可以忽略它。此外,在枚举节点时,很容易询问引用的节点,获取一个或者什么都不需要,而不必先查看节点是否实际上是referenceNode,然后执行更改。