用 SpriteKit 在矩形上画一个洞? [英] Draw a hole in a rectangle with SpriteKit?

查看:16
本文介绍了用 SpriteKit 在矩形上画一个洞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在矩形上画一个洞?我想画一个矩形,里面有一个空洞,可以显示背景.

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.

我正在使用 SKShapeNode 创建一个矩形,但我不知道如何在其中制作一个孔(圆).

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?

推荐答案

这里是代码.

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)

    }
}

如您所见,我创建了一个 SKEffectNode.然后我添加到它的矩形.最后,我将孔添加到矩形中.

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

这篇关于用 SpriteKit 在矩形上画一个洞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆