Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preload SKEmitterNode, has lag when using it

this is how i put my emitter:

func addParticle(at: CGPoint) {
        let emitter = SKEmitterNode(fileNamed: "hit.sks")
        emitter?.position = at
        emitter?.zPosition = 10
        scene.addChild(emitter!)
        scene.run(SKAction.wait(forDuration: 1), completion: {
            emitter?.removeFromParent()
        })
    }

and sometimes i have a performance lag, time profiler shows me that i am having sks file delay (file decoding etc). is there any way i can avoid this?

like image 551
Anton Shevtsov Avatar asked Apr 18 '26 07:04

Anton Shevtsov


1 Answers

You're not actually preloading the particle system. You're creating a new one, each time, and removing it (and causing there to be no reference to it) at the end, so it gets GC'd.

Instead, add the particle system to a node that's offscreen, and when you need it, move it back into the scene, where you need/want it, then move it back offscreen when you no longer need it.

This will prevent any need to create a particle system, wind it up and get it running, etc.

You'll just need to play and pause it... and move it.

You can pause a particle system directly, or by pausing its parent node, so it's ready at a state you want it to be in when you bring it back onscreen.

Read about more of this here: https://developer.apple.com/documentation/spritekit/skemitternode/1398027-advancesimulationtime

like image 160
Confused Avatar answered Apr 20 '26 03:04

Confused