How to launch sparkEmmiter (created custom smoke.sks file) in the view? Swift 5

220 Views Asked by At

I have created the SpriteKit Particle Emitter file smoke.sks. enter image description here I want to make this animation run in a UIView, however, it has no addChild method and I still have some errors...

I have tried this solution:

func addSmoke() {

    //spark particle effect
    let sparkEmmitterPath: String = Bundle.main.path(forResource: "smoke", ofType: "sks")!
    let sparkEmmiter = NSKeyedUnarchiver.unarchiveObject(withFile: sparkEmmitterPath as String) as! SKEmitterNode

    sparkEmmiter.position = CGPoint(x: self.backgroundMaskView.frame.size.width, y: self.backgroundMaskView.frame.size.height/2)
    sparkEmmiter.name = "smoke"
    sparkEmmiter.zPosition = 1
    sparkEmmiter.targetNode = self
    self.view.addChild(sparkEmmiter)
}

2 errors:

sparkEmmiter.targetNode = self

Cannot assign value of type 'ViewController' to type 'SKNode?'

self.addChild(sparkEmmiter)

Cannot convert value of type 'SKEmitterNode' to expected argument type 'UIViewController'

Should I also create SpriteKit View (SKView)?

1

There are 1 best solutions below

0
On BEST ANSWER

The only solutions I have found. We need to change the UIView class to SCNView and create scnp file for particles.

@IBOutlet weak var particles: SCNView!

override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene()        
    let particlesNode = SCNNode()
    let particleSystem = SCNParticleSystem(named: "smoke.scnp", inDirectory: "")
    particlesNode.addParticleSystem(particleSystem!)
    scene.rootNode.addChildNode(particlesNode)
    particles.backgroundColor = .clear
    particles.scene = scene
}

But ... Apple did removed the ability to create scnp files.

The second step is to change the type of the file. Create SpriteKit Particle File

(do not configure this created file, because you will have to do it again when you change the type, it will reset)

Now, change the Identity type. enter image description here