Add SKReferenceNode/SKScene to another SKScene in SpriteKit

后端 未结 2 637
心在旅途
心在旅途 2020-12-17 03:43

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

相关标签:
2条回答
  • 2020-12-17 04:07

    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.

    0 讨论(0)
  • 2020-12-17 04:10

    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

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

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

    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:

    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}
        }
    }
    
    0 讨论(0)
提交回复
热议问题