为 SKSpriteNode 添加发光效果 [英] Add glowing effect to an SKSpriteNode

查看:19
本文介绍了为 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", parameters: ["inputRadius":radius])
    }
}

现在给定一个 SKSpriteNode

Now given an SKSpriteNode

let sun = SKSpriteNode(imageNamed: "sun")

你必须做的一切

sun.addGlow()

这篇关于为 SKSpriteNode 添加发光效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆