Add SKReferenceNode/SKScene to another SKScene in SpriteKit
Asked Answered
P

2

5

I would like to add a SKScene to my main GameScene. SKReferenceNode seems to be a good solution.

I have : - GameScene.sks (main scene) - Countdown.sks (scene to add to GameScene) - Countdown.swift (Custom class, how does to init it? SKScene ? SKReferenceNode ? SKNode)

I don't know how to add programmatically my countdown using my class Countdown.

I tried:

 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)

But I have the following error :

 Could not cast value of type 'SKReferenceNode' (0x10d97ad88) to 'LYT.Countdown' (0x10a5709d0

I also tried something more simple like:

 let cd=Countdown(scene:self) 
 self.addChild(cd)

But I don't know how to init the class using the Countdown.sks file.

I know I also have the possibility to create a SKNode class, and init it 100% programmatically, but it really important for me to use the associated .sks file in order to use the Xcode scene editor.

Present answered 8/9, 2016 at 12:2 Comment(2)
Sounds like you Countdown is using the func update? Because you can't add 2 SKScene in one, but you can subclass Countdown like SKSpriteNode or SKNode and use it inside SKReferenceNodeSaveloy
@SimonePistecchia I tried "class Countdown:SKReferenceNode", if I subclass it in SKNode, how can I do to create and init my node using the sks file?Present
S
9

I do that, I don't know if is the best way to do this, but works:

I've 2 file Dragon.swift and sks

enter image description here

I've added a "main" node like DragonNode and other node children of this

enter image description here

Now, the DragonNode is a custom class, set it in sks file:

enter image description here

The DragonNode is a normal 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 () {}
}

Inside the scene, add a SKReferenceNode:

enter image description here

In the SKScene code:

    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() is an extension to find your based node (the first one)

extension SKReferenceNode {
    func getBasedChildNode () -> SKNode? {
        if let child = self.children.first?.children.first {return child}
        else {return nil}
    }
}
Saveloy answered 8/9, 2016 at 17:9 Comment(2)
Interesting extensionSeraphic
@Simone Pistecchia, sorry for taking all your time, i found the problem, i used a protocol to load all the nodes declared in a scene, which it was just one time time use :/ that was it, thanks again for your time and help.Strachan
B
1

I do a similar thing to Simone above, but instead of extending the reference node, I added my extension to SKNode.

extension SKNode {
    func nodeReferenced() -> SKNode? {
        if self .isKind(of: SKReferenceNode.self) {
            return children.first!.children.first!
        }
        return nil
    }
}

This way there is no need for the cast if the node is not actually a reference node and make this two step process a one liner. My version would change that above code to:

if let dragonNode = childNodeWithName("DragonReference")?.nodeReferenced() as? DragonNode {
    print(dragonNode)
    dragonNode.fly()
}

This works for me, but Simone's answer seems more straight forward and maybe flexible than mine, so I'd give them the points. I just like clean code and since we almost never actually need that SKReferenceNode, we can ignore it. Also, when enumerating the nodes, it's easy to ask for a referenced node, get one or nothing, without having to see if the node is actually a referenceNode first, then performing the change.

Bornu answered 19/4, 2019 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.