Draw a hole in a rectangle with SpriteKit?

China☆狼群 提交于 2019-12-17 19:03:19

问题


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?


回答1:


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)

    }
}

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



来源:https://stackoverflow.com/questions/34576112/draw-a-hole-in-a-rectangle-with-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!