我想达到此gif所示的效果。
目前,我使用一系列带有红色背景和白线的〜7 png图像来完成此操作,这些图像使用SKAction通过精灵进行动画处理。
我想对精灵进行其他一些添加,这些添加可以根据情况而变化,并且我想用多种颜色重复进行。 结果是:6种颜色,7种发光效果,5种边缘效果和4种角落效果产生了我需要创建和存储的136种纹理组合。
我觉得在设置sprite的纹理时必须有一种方法可以将png与透明背景叠加,但是我似乎找不到在任何地方进行此操作的方法。
是否有可能使我将资产数量减少到22个,或者我必须全部制作136个资产并在类中建立逻辑来决定使用哪个资产?
答案 0 :(得分:1)
我想为我的游戏提供这样的效果,我尝试了很多选择。我尝试使用粒子来提高性能,但是甚至无法接近。我知道您可以使用Shaders来完成它,但是我没有走那条路,在iOS 12中,Shaders无论如何都不支持Open GL。最后,我选择了使用CropNodes。
这是我的眩光图像,很难看到,因为它略带透明的白色图像。
这是我使用CropNodes实现的结果
class Glare: SKSpriteNode {
var glare = SKSpriteNode()
private var cropNode = SKCropNode()
init(color: UIColor, size: CGSize) {
super.init(texture: nil, color: color, size: size)
alpha = 0.7
zPosition = 10
setupGlare()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupGlare() {
let buffer: CGFloat = 10
let mask = SKSpriteNode(texture: nil, color: .black, size: self.size)
let texture = SKTextureAtlas(named: "Sprites").textureNamed("glare")
glare = SKSpriteNode(texture: texture)
glare.position = CGPoint(x: 0 - (self.size.width / 2 + buffer), y: self.size.height / 2 + buffer)
glare.setScale(3.50)
glare.zPosition = 1
cropNode.zPosition = 2
cropNode.maskNode = mask
cropNode.addChild(glare)
let random = Double(CGFloat.random(min: 0, max: 1))
let pause = SKAction.wait(forDuration: random)
let move = SKAction.moveBy(x: self.size.width + buffer * 2, y: 0 - (self.size.height + buffer * 2), duration: 0.5)
let wait = SKAction.wait(forDuration: 1.0)
let reset = SKAction.moveBy(x: 0 - (self.size.width + buffer * 2), y: self.size.height + buffer * 2, duration: 0.0)
let seq = SKAction.sequence([move, wait, reset])
let repeater = SKAction.repeatForever(seq)
let repeaterSeq = SKAction.sequence([pause, repeater])
glare.run(repeaterSeq)
}
func showGlare(texture: SKTexture) {
let mask = SKSpriteNode(texture: texture)
cropNode.maskNode = mask
glare.isHidden = false
if cropNode.parent == nil { self.addChild(cropNode)}
}
func hideGlare() {
glare.isHidden = true
//remove cropnode from the node tree
cropNode.removeFromParent()
}
}
然后在我的GameScene中...
我将眩光添加到眩光层中,但这不是必需的。我还会在游戏加载时进行检查,并提前为所有15个插槽创建眩光并将它们排列成阵列。这样,我就不必立即创建它们,我可以随时随地打开插槽10,也可以将其关闭。
private var glares = [Glare]()
let glare = Glare(color: .clear, size: CGSize(width: kSymbolSize, height: kSymbolSize))
glare.position = CGPoint(x: (CGFloat(x - 1) * kSymbolSize) + (kSymbolSize / 2), y: 0 - (CGFloat(y) * kSymbolSize) + (kSymbolSize / 2))
glare.zPosition = 100
glareLayer.addChild(glare)
glares.append(glare)
当我想在插槽上显示眩光时
在这里为您编辑纹理只是一个与瓷砖大小相同的空白正方形纹理。
glares[index].showGlare(texture: symbol.texture!)
当我想隐藏它时
glares[index].hideGlare()