gpt4 book ai didi

swift 3 : cut a hole in a SKSpriteNode

转载 作者:行者123 更新时间:2023-11-28 15:24:55 30 4
gpt4 key购买 nike

首先:我知道,这个问题在这里已经有很多答案了,但是他们并没有帮助我解决这个问题。

我编写了一个小游戏。在第一次发布时有一个小教程,其中一点一点地解释了游戏的每个元素。在每一步中,我都想强调这些元素之一。所以我在元素前面放了一个alpha为0.9的黑色SKSpriteNode。如果一个元素应该被高亮显示,此时 SpriteNode 应该变成透明的。所以我制作了一个 SKCropNode 和一个 mask ,如下面的代码所示(我只向您展示了基本部分):

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 上,除了亮点外,整个应用程序运行完美。

图片:

One tutorial-point on the simulator

The same on the iPhone

你能告诉我为什么不同吗?我以前从未见过这样的事情。

最佳答案

我遇到了同样的问题。我花了 3 周的时间才找到答案。

将孔的 aplha 设置为 0.001,将混合模式设置为 .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)
}
}

关于 swift 3 : cut a hole in a SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406414/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com