szilagyif
szilagyif

Reputation: 145

SKCropNode not working properly in Xcode 7 + Swift 2

In my SpriteKit game, I have a scrollable node which has a crop node, so it is not visible in the whole screen. I made a node with black color and a very small alpha value and it worked than I expected.

    let rectNode = SKSpriteNode(color: UIColor(red: 0, green: 0, blue: 0, alpha: 0.000001), size: CGSize(width: width, height: height - 100) )
    rectNode.position = CGPoint(x: 0, y: 0)
    rectNode.zPosition = 1
    rectNode.anchorPoint =  CGPoint(x: 0, y: 0)                

    rectNode.addChild(contentNode)        

    let cropNode = SKCropNode()
    cropNode.maskNode = rectNode.copy() as! SKSpriteNode
    cropNode.addChild(rectNode)
    cropNode.zPosition = 9

    addChild(cropNode)

Since XCode 7 my content node is not visible, if I change rectNode's alpha to 1, the content is visible with black background, so I think something happened with masking.

Am I doing something wrong or is it a bug in Xcode 7?

Upvotes: 0

Views: 950

Answers (1)

Naftali Beder
Naftali Beder

Reputation: 1076

Looking at your code, I'm not actually sure how it worked in Xcode 6. Since the mask is a rectangle with almost zero alpha, it shouldn't actually prevent any of the SKCropNode's pixels from being displayed - that is, it should be having no effect. From the docs:

If the pixel in the mask has an alpha value of less than 0.05, the image pixel is masked out.

All of the pixels in your mask node have alpha < 0.05, so the entirety of rectNode should be invisible.

It would help to give them clearer names, and create your visible content and the mask separately. Try something like this:

let contentNode = SKSpriteNode()  // Whatever you want to have displayed and cropped.
// adjust contentNode's position and size

let cropNode = SKCropNode()
// adjust cropNode's position and size

let maskNode = SKSpriteNode()
// adjust maskNode's position and size
maskNode.color = UIColor.blackColor()  // If you're not going to use an image for this, you need to fill it with a color. The black part will act as a "window", with everything outside of it being invisible.

cropNode.maskNode = maskNode
cropNode.addChild(contentNode)

Upvotes: 2

Related Questions