Cropping SKTexture with SKLabel text

To create a button in SpriteKit, I created a rounded rectangle with SKShapeNode, and then placed SKLabel above it.

It looks like this: enter image description here

I want to have a background that is under a label change with what's under it. Thus, inside the image, T, E and half A will be purple.

Is there a way to drag the font halfway? Or somehow crop the texture of the white rectangle with the SKLabel that is on it. Thus, there is no label, and instead a label is cut from the texture. Basically, I want the font color to be what stands behind it.

I need to do this without importing images and code in some way.

+4
1

Knight0fDragon , . , textureFromNode. cropNode , :

import SpriteKit
class GameScene: SKScene {
    override func didMove(to view: SKView) {
        let background = SKSpriteNode.init(imageNamed: "background.png")
        background.zPosition = 1
        let background2 = background.copy() as! SKSpriteNode
        //label
        let titleLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
        titleLabel.fontSize = 150
        titleLabel.fontColor = SKColor.black
        titleLabel.text = "RATE"
        titleLabel.horizontalAlignmentMode = .center
        titleLabel.zPosition = 5
        titleLabel.blendMode = .subtract
        //cropNode
        let cropNode = SKCropNode()
        cropNode.addChild(background)
        cropNode.maskNode = titleLabel
        self.addChild(cropNode)
        cropNode.zPosition = 5
        //button
        let button = SKShapeNode(rect: CGRect(x: -300, y: -50, width: 600, height: 200), cornerRadius: 100)
        button.zPosition = 4
        button.fillColor = SKColor.white
        self.addChild(background2)
        self.addChild(button)
    }
}

:

:

2:

+4

Source: https://habr.com/ru/post/1674255/


All Articles