1

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
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • Well I managed to work around this by using a crop node, but i would still like to know why the scene size parameters do not apply. I know the reference node itself doesn't have a size property, but that just makes me think it should use the size from the scene assigned to it. – TheValyreanGroup May 23 '17 at 14:15
  • why would the scene size paramaters apply? scene does not do any cropping whatsoever, so just because you do not see them, does not mean they should crop – Knight0fDragon May 23 '17 at 14:50
  • Ok. I just assumed if a scene was setup like the 2nd picture, it would cropToBounds, like it would in UIKit. – TheValyreanGroup May 23 '17 at 14:55
  • negative, the scene size should be interpreted as the viewport, you will have items that exist outside of the viewport, but you do not want them cropped – Knight0fDragon May 23 '17 at 14:57
  • Understood. Thanks. – TheValyreanGroup May 23 '17 at 14:58

1 Answers1

0

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

Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30