一尘不染

向SKSpriteNode添加发光效果

swift

我在黑屏上有一个移动的黑色图像,以使其更易于查看,我想在图像中添加白色光晕。这是我的运动图像代码:

   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)

我不确定如何添加发光效果,如果您需要更多信息,请询问。


阅读 277

收藏
2020-07-07

共1个答案

一尘不染

我创建了此扩展以向SKSpriteNode添加发光效果

只需将其添加到您的项目中

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

let sun = SKSpriteNode(imageNamed: "sun")

所有你需要做的

sun.addGlow()
2020-07-07