Cropping an SKReferenceNode

97 views Asked by At

I have setup a SKScene to be used as an SKReferenceNode. The sled is an SKSpriteNode with a custom class defined in the reference node scene and all the dogs are children of the sled sprite. The custom class and using the reference node is all fine, my problem is i'm unable to "crop" the scene to only show N number of dogs. Its as if after dropping the reference node into another scene, that parent scene is ignoring the reference node's width/height parameters and just displaying everything in it. So the question is, is this possible? Or do SKReferenceNodes not adhere to the scene width and heights properties when used in a parent scene?

The first image is of the full view reference scene (70x425). Second image is of what the frame should look like when i change the height programmatically if I only want the bottom 2 dogs to be shown.

enter image description hereenter image description here

class SledTeam: SKSpriteNode {
    var dogTeam = [Int]()
    required init?(coder aDecoder: NSCoder) {
        switch dogTeam.count {
            case 7,8:
                self.scene?.size.height = 425
                break
            case 5,6:
                self.scene?.size.height = 335
                break
            case 3,4:
                self.scene?.size.height = 260
                break
            case 1,2:
                self.scene?.size.height = 190
                break
            default:
                break
        }
    }
}
1

There are 1 answers

0
Simone Pistecchia On

My gues is to use a invisible sprite node in your sks reference file with the size to contains all nodes. After call this invisible "base" node and crop it. To get the invisible node, you can use this extension:

extension SKReferenceNode {
    func getBasedChildNode () -> SKNode? {
        if let child = self.children.first?.children.first {return child}
        else {return nil}
    }
}

for other details see my old post: Add SKReferenceNode/SKScene to another SKScene in SpriteKit