使SKLabelNode成为SKShapeNode的裁剪节点

时间:2016-03-14 11:30:27

标签: swift sprite-kit skshapenode sklabelnode skcropnode

无法在网络上找到如何使SKLabelNode裁剪SKShapeNode。当我为背景着色时,我的目标是使用相同的方法着色标签,因此它们都必须同时着色。但无法想象如何使用此标签裁剪SKShapeNode。请帮帮我!

1 个答案:

答案 0 :(得分:1)

  

但无法想象如何使用此标签裁剪SKShapeNode。

如果我理解正确,您可以将SKLabelNode设置为SKCropNode的掩码,如下所示:

override func didMoveToView(view: SKView) {

        backgroundColor = .blackColor()

        let cropNode = SKCropNode()
        cropNode.position = CGPoint(x: frame.midX, y: frame.midY)
        cropNode.zPosition = 1

        let mask = SKLabelNode(fontNamed: "ArialMT")
        mask.text = "MASK"
        mask.fontColor = .greenColor()
        mask.fontSize = 28

        cropNode.maskNode = mask


        let nodeToMask = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 200, height: 200))
        nodeToMask.position = CGPoint(x: 0, y: 0)
        nodeToMask.name = "character"
        cropNode.addChild(nodeToMask)
        //Now colorize the sprite which acts like background
        let colorize = SKAction.sequence([
                SKAction.colorizeWithColor(.orangeColor(), colorBlendFactor: 0, duration: 1),
                SKAction.colorizeWithColor(.purpleColor(), colorBlendFactor: 0, duration: 1)
        ])

        nodeToMask.runAction(SKAction.repeatActionForever(colorize), withKey: "colorizing")

        addChild(cropNode)

}

结果:

mask