简体   繁体   English

SKCropNode在Xcode 7 + Swift 2中无法正常工作

[英]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. 在我的SpriteKit游戏中,我有一个带有裁切节点的可滚动节点,因此在整个屏幕上都不可见。 I made a node with black color and a very small alpha value and it worked than I expected. 我用黑色和很小的alpha值制作了一个节点,它的工作超出了我的预期。

    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. 由于XCode 7我的内容节点不可见,因此,如果我将rectNode的alpha更改为1,则该内容在黑色背景下可见,因此我认为屏蔽发生了某些事情。

Am I doing something wrong or is it a bug in Xcode 7? 我是在做错什么还是Xcode 7中的错误?

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. 查看您的代码,我实际上不确定它在Xcode 6中的工作方式。由于该遮罩是具有几乎为零的alpha值的矩形,因此它实际上不应阻止任何SKCropNode像素的显示-也就是说,它应该是没有效果。 From the docs : 文档

If the pixel in the mask has an alpha value of less than 0.05, the image pixel is masked out. 如果蒙版中的像素的alpha值小于0.05,则将图像像素蒙版。

All of the pixels in your mask node have alpha < 0.05, so the entirety of rectNode should be invisible. 遮罩节点中的所有像素的alpha均小于0.05,因此rectNode的整体应该是不可见的。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM