15

除了这里提出的问题:用 SpriteKit 在矩形中画一个洞?本身并没有得到令人满意的回答,两者之间最显着的区别是这个问题需要没有变通方法,并且专门询问反转 SKCropNodes 的功能。

由于孔的随机性、孔的数量以及要应用孔的对象的变化,上述问题的回答方式无法解决这个问题的主要问题。

因此瑞士奶酪的例子:

想象一堆随机大小的矩形 SKSpriteNodes,里面充满了各种深浅不一的奶酪状颜色。

如何通过从每片奶酪上随机切出圆形来制作瑞士奶酪?

如果 SKCropNodes 是圆圈,它们只会留下圆形的奶酪片,而不是从奶酪片上切出洞。有没有办法反转 SKCropNodes 的行为,以便它们切孔?

4

1 回答 1

8

老实说,我不确定是否能深入理解您想要实现的目标,但我可以尝试回答这个问题:

..从每片奶酪上随机切出圆圈?

在这个项目中,我尝试重新构建一个带有随机孔的典型矩形(一块奶酪),然后我提取这些孔并将它们收集到一个数组中。

import SpriteKit
class GameScene: SKScene {
    struct Cheese
    {
        static let color1 = SKColor(red: 255/255, green: 241/255, blue: 173/255, alpha: 1)
        static let color2 = SKColor(red: 255/255, green: 212/255, blue: 0/255, alpha: 1)
        static let color3 = SKColor(red: 204/255, green: 170/255, blue: 0/255, alpha: 1)
        static let color4 = SKColor(red: 140/255, green: 116/255, blue: 0/255, alpha: 1)
    }
    let cheeseColor = [Cheese.color1,Cheese.color2,Cheese.color3,Cheese.color4]
    override func didMove(to view: SKView) {        
        let totHoles = randomNumber(range:4...8)
        let color = randomNumber(range:0...3)
        let cheeseCropNode = makeCheese(size: CGSize(width:400,height:200),color: cheeseColor[color], totHoles:totHoles)
        cheeseCropNode.position = CGPoint(x:0,y:-50)
        addChild(cheeseCropNode)
        // Start to collect and show holes
        var holes = [SKNode]()
        var counter = 1
        let _ = cheeseCropNode.enumerateChildNodes(withName: "//hole*", using:{ node, stop in
            // node is the hole
            let pos = self.convert(node.position, from: cheeseCropNode)
            let sprite = SKSpriteNode.init(color: .red, size: node.frame.size)
            sprite.position = pos

            //Remove these shapes, it's just to debug
            let shape = SKShapeNode.init(rect: sprite.frame)
            shape.strokeColor = .red
            self.addChild(shape)
            // -- end to remove

            let holeTxt = SKView().texture(from: cheeseCropNode, crop: sprite.frame)
            let hole = SKSpriteNode.init(texture: holeTxt)
            hole.position = CGPoint(x:-(self.frame.maxX)+(100*CGFloat(counter)),y:150)
            hole.name = node.name
            self.addChild(hole)
            holes.append(hole)
            counter += 1
        })
    }

    func randomNumber(range: ClosedRange<Int> = 1...6) -> Int {
        let min = range.lowerBound
        let max = range.upperBound
        return Int(arc4random_uniform(UInt32(1 + max - min))) + min
    }
    func randomCGFloat(min: CGFloat, max: CGFloat) -> CGFloat {
        return (CGFloat(arc4random()) / CGFloat(UINT32_MAX)) * (max - min) + min
    }
    func makeCheese(size:CGSize , color:SKColor, totHoles:Int)->SKCropNode {
        let cropNode = SKCropNode()
        let cheese = SKSpriteNode.init(color: color, size: size)
        for i in 0..<totHoles {
            let radius = randomCGFloat(min:20.0, max:50.0)
            let circle = SKShapeNode(circleOfRadius: radius)
            circle.position = CGPoint(x:randomCGFloat(min:-size.width/2, max:size.width/2),y:randomCGFloat(min:-size.height/2, max:size.height/2))
            circle.fillColor = color
            circle.blendMode = .subtract
            circle.name = "hole\(i)"
            cheese.addChild(circle)
        }
        cropNode.addChild(cheese)
        cropNode.maskNode = cheese
        return cropNode
    }
}

结果

PS不要注意红色矩形,只是给你看洞:

在此处输入图像描述

如果您想要完全反转的孔(负图像),您可以使用SKCropNodehole.blendMode例如:

替换这部分代码:

// -- end to remove

                let holeTxt = SKView().texture(from: cheeseCropNode, crop: sprite.frame)
                let hole = SKSpriteNode.init(texture: holeTxt)
                hole.position = CGPoint(x:-(self.frame.maxX)+(100*CGFloat(counter)),y:150)
                hole.name = node.name
                self.addChild(hole)
                holes.append(hole)
                counter += 1

这部分:

// -- end to remove

            let holeTxt = SKView().texture(from: cheeseCropNode, crop: sprite.frame)
            let hole = SKSpriteNode.init(texture: holeTxt)
            hole.position = CGPoint(x:-(self.frame.maxX)+(100*CGFloat(counter)),y:150)
            hole.name = node.name

            let negativeCropHole = SKCropNode()
            let shadow = SKShapeNode.init(rect: hole.frame)
            shadow.fillColor = (node as! SKShapeNode).fillColor
            shadow.strokeColor = SKColor.clear
            hole.blendMode = .subtract
            negativeCropHole.addChild(shadow)
            negativeCropHole.maskNode = shadow
            negativeCropHole.addChild(hole)
            negativeCropHole.name = hole.name
            self.addChild(negativeCropHole)
            holes.append(negativeCropHole)
            counter += 1

结果(另一个例子):

在此处输入图像描述

希望这些示例和此代码可以帮助您实现目标,我使用矩形制作蒙版,但您可以根据需要创建 CGPath。

于 2016-11-01T17:10:59.963 回答