我有一个简单的场景,添加了一些元素。
现在我想专注于一个带有遮罩的特定元素,在与我想要关注的元素相同的位置切割整个元素。与我们在某些游戏第一次启动时看到的显示某种教程非常相似。
基本上我添加了一个全屏层alpha=0.7
(因此用户仍然可以看到所有内容),但然后在特定位置添加一个圆圈作为该图层的子层并设置blendMode=
。减去,这样它就会从这个全屏图层中“切出”一个圆圈,这样在这个圆圈内你就有一个清晰的视图。
将所有元素添加到屏幕上后,我有以下代码。
// before this code i added some basic elements like circles and backgrounds
let mask = SKSpriteNode(color: .blackColor(), size: self._screenSize)
mask.anchorPoint = CGPoint.zero
mask.position = CGPoint.zero
mask.zPosition = 100
mask.alpha = 0.7
let circle = SKShapeNode(circleOfRadius: Constants.Config.playersize*2)
circle.fillColor = .blackColor()
circle.lineWidth = 0
let circle_mask = SKSpriteNode(texture: SKView().textureFromNode(circle, crop: circle.frame))
circle_mask.blendMode = .Subtract
circle_mask.zPosition = 101
// now show the layer with alpha=0.7 and the circle mask being at the same position as my player element i want to focus on
mask.addChild(circle_mask)
circle_mask.position = player.position
self.addChild(mask)
但这只是添加了全屏层,没有圆孔。看起来它忽略了circle_mask 节点。我究竟做错了什么?
我的计划还包括继续移动圆形遮罩以关注该场景中的其他元素。我认为。减去它应该仅从其父节点中减去,该父节点是全屏层alpa=0.7
, right?
我刚刚尝试过 SKCropNode。将全屏图层作为子图层添加到裁剪节点中,然后将圆圈指定为遮罩。但现在它几乎剪掉了所有东西,只显示了我的全屏图层的一个圆圈,我实际上需要这个裁剪节点的反转结果。
BlendMode 的问题是在最终帧缓冲区上运行它,但我需要的是仅在父节点上运行它,因此在使用 .Subtract 时它不会剪切节点后面的所有内容
以下是如何创建带有打孔的半透明层的示例:
-(注意:.subtract混合模式不考虑精灵的alpha,因此您需要使用裁剪节点)
self.backgroundColor = .red
//Our transparent overlay
let fullScreen = SKSpriteNode(color: .black, size: self.size)
fullScreen.position = CGPoint.zero
fullScreen.zPosition = 100
fullScreen.alpha = 0.7
//let's make a mask to punch circles in our shape
let mask = SKSpriteNode(color: .white, size: self.size)
mask.position = CGPoint.zero
mask.zPosition = 100
mask.alpha = 1
let circle = SKShapeNode(circleOfRadius: 20*2)
circle.fillColor = .white
circle.lineWidth = 0
circle.alpha = 1
//let circle_mask = SKSpriteNode()
circle.blendMode = .subtract
mask.addChild(circle)
//let's create the node to place on screen
let crop = SKCropNode()
crop.maskNode = mask
crop.addChild(fullScreen)
self.addChild(crop)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)