Draw a hole in a rectangle with SpriteKit?
Asked Answered
C

1

6

How to draw a hole in a rectangle? I want to draw a rectangle which has a empty hole inside it and the background can be show.

I am using SKShapeNode to create a rectangle, but I have no idea how to make a hole (circle) inside it.


This is what I run your code, but my circle is not empty, the circle is black, I want it to be empty. Is there any mistake I didn't mention? enter image description here

Chairman answered 3/1, 2016 at 11:41 Comment(3)
Take a look at SKCropNodeRutty
Thanks~ SKCropNode works!!Chairman
@Yan-JenHuang Hey, could you please share your code with SKCropNode? ThanksTijuana
F
2

Here's the code.

import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        let effect = SKEffectNode()
        addChild(effect)

        let rect = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 400, height: 200))
        rect.fillColor = .green
        effect.addChild(rect)


        let hole = SKShapeNode(circleOfRadius: 40)
        hole.position = CGPoint(x: 200, y: 100)
        hole.fillColor = .white
        hole.blendMode = .subtract
        rect.addChild(hole)

    }
}

enter image description here

As you can see I create an SKEffectNode. Then I add to it the rectangle. Finally I add the hole to the rectangle.

Ferreous answered 3/1, 2016 at 13:0 Comment(5)
I still have some problem here, I want the circle to be empty!Chairman
how to use the .Subtract method? what is the different if i choose other color to rect or circleChairman
@Yan-JenHuang: ops... let me check it :DFerreous
@Yan-JenHuang: Fixed! Please take a look.Ferreous
Doesn't work for me. All it does is subtract color, ie renders it black. I want to see what's beneath the green rectangle, like looking through a hole, ie a cutuot.Rhineland

© 2022 - 2024 — McMap. All rights reserved.