将发光效果添加到SKSpriteNode [英] Add glowing effect to an SKSpriteNode
本文介绍了将发光效果添加到SKSpriteNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在黑暗的屏幕上有一个移动的黑色图像,以便更容易看到我想为图像添加白色光晕。这是我的动态图像代码:
I have a moving black image on a dark screen, to make it easier to see I would like to add in a white glow to the image. This is my code for the moving image:
Ghost = SKSpriteNode(imageNamed: "Ghost1")
Ghost.size = CGSize(width: 50, height: 50)
Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
Ghost.physicsBody?.affectedByGravity = false
Ghost.physicsBody?.isDynamic = true
Ghost.zPosition = 2
self.addChild(Ghost)
如果您需要更多信息,我不知道如何或用什么来添加光晕请问。
I'm not sure how or what to use to add in a glow, if you need more information please ask.
推荐答案
我创建了这个扩展,为SKSpriteNode添加了一个发光效果
I created this extension to add a glow effect to an SKSpriteNode
只需将其添加到您的项目中
Just add this to your project
extension SKSpriteNode {
func addGlow(radius: Float = 30) {
let effectNode = SKEffectNode()
effectNode.shouldRasterize = true
addChild(effectNode)
effectNode.addChild(SKSpriteNode(texture: texture))
effectNode.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius":radius])
}
}
现在给出SKSpriteNode
Now given an SKSpriteNode
let sun = SKSpriteNode(imageNamed: "sun")
所有你必须这样做
sun.addGlow()
< a href =https://i.stack.imgur.com/3t7IZ.png>
这篇关于将发光效果添加到SKSpriteNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文