用SpriteKit在矩形中画一个洞?

时间:2023-02-09 23:39:44

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.

我正在使用SKShapeNode创建一个矩形,但我不知道如何在其中创建一个洞(圆圈)。


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? 用SpriteKit在矩形中画一个洞?

这是我运行你的代码,但我的圈子不是空的,圆圈是黑色的,我希望它是空的。我没有提到任何错误吗?

1 个解决方案

#1


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)

    }
}

用SpriteKit在矩形中画一个洞?

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

如您所见,我创建了一个SKEffectNode。然后我添加矩形。最后,我将洞添加到矩形。

#1


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)

    }
}

用SpriteKit在矩形中画一个洞?

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

如您所见,我创建了一个SKEffectNode。然后我添加矩形。最后,我将洞添加到矩形。