簡體   English   中英

Swift 3:在SKSpriteNode上打個洞

[英]Swift 3: cut a hole in a SKSpriteNode

首先:我知道,這個問題在這里已經有很多答案,但是它們並沒有幫助我解決這個問題。

我編寫了一個小游戲。 在首次發布時,有一個小教程,其中逐步解釋了游戲的每個元素。 在每一步中,我要強調這些元素之一。 因此,我在元素前面放置了一個黑色的SKSpriteNode,其alpha值為0.9。 如果應突出顯示某個元素,則此時SpriteNode應該變為透明的。 因此,我制作了一個SKCropNode和一個蒙版,如下面的代碼所示(我僅向您展示了必要的部分):

import SpriteKit

class FirstLaunch: SKScene {

    var fullScreen:SKSpriteNode!
    var mask:SKSpriteNode!
    var circle1:SKShapeNode!
    var circle2:SKShapeNode!
    var crop:SKCropNode!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(size: CGSize) {
        super.init(size: size)

        fullScreen = SKSpriteNode(color: .black, size: self.size)
        fullScreen.anchorPoint = .zero
        fullScreen.position = .zero
        fullScreen.alpha = 0.9

        mask = SKSpriteNode(color: .white, size: self.size)
        mask.anchorPoint = .zero
        mask.position = .zero
        mask.alpha = 1

        circle1 = SKShapeNode(circleOfRadius: 55)
        circle1.fillColor = .white
        circle1.lineWidth = 0
        circle1.alpha = 1
        circle1.blendMode = .subtract

        //spaceship is one of my elements, which have to be highlighted at some point
        circle1.position = spaceship.position
        mask.addChild(circle1)

        //At one point I need two highlights at the same time
        circle2 = SKShapeNode(circleOfRadius: 55)
        circle2.fillColor = .white
        circle2.lineWidth = 0
        circle2.alpha = 1
        circle2.blendMode = .subtract

        crop = SKCropNode()
        crop.maskNode = mask
        crop.addChild(fullScreen)

        addChild(crop)
    }
}

我在這里找到此解決方案: https : //stackoverflow.com/a/40710050/8162321我在不同的設備和模擬器上對其進行了測試。 我的問題是,它在Xcode 8和Xcode 9 Beta的模擬器中都可以完美運行,但在裝有iOS 10.3.3的iPhone和裝有iOS 11 Beta的iPhone上都無法運行。 在iPhone上,除了亮點外,整個應用程序都運行良好。

圖片:

模擬器上的一個教程點

在iPhone上也一樣

你能告訴我為什么不同嗎? 我以前從未見過這樣的東西。

我有同樣的問題。 我花了三個星期才找到答案。

將孔的aplha設置為0.001,將blendmode設置為.replace。
然后它將在設備上運行(對我而言,在iOS 12上)。

import SpriteKit

class FirstLaunch: SKScene {

    var fullScreen:SKSpriteNode!
    var mask:SKSpriteNode!
    var circle1:SKShapeNode!
    var circle2:SKShapeNode!
    var crop:SKCropNode!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(size: CGSize) {
        super.init(size: size)

        fullScreen = SKSpriteNode(color: .black, size: self.size)
        fullScreen.anchorPoint = .zero
        fullScreen.position = .zero
        fullScreen.alpha = 0.9

        mask = SKSpriteNode(color: .white, size: self.size)
        mask.anchorPoint = .zero
        mask.position = .zero
        mask.alpha = 1

        circle1 = SKShapeNode(circleOfRadius: 55)
        circle1.fillColor = .white
        circle1.lineWidth = 0

        // Here is the required change
        circle1.alpha = 0.001
        circle1.blendMode = .replace

        //spaceship is one of my elements, which have to be highlighted at some point
        circle1.position = spaceship.position
        mask.addChild(circle1)

        crop = SKCropNode()
        crop.maskNode = mask
        crop.addChild(fullScreen)

        addChild(crop)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM