Anton Shevtsov
Anton Shevtsov

Reputation: 190

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?

Upvotes: 3

Views: 364

Answers (2)

Anton Shevtsov
Anton Shevtsov

Reputation: 190

ok so i sorted it out by creating a particle manager that creates all the needed particles for the game. and when i need one of them i just use method .copy() as? SKEmitterNode. of course particle manager is a smart class that doing all the work (resetting animation and starting) and providing ready to use emitter. this way - no lag, no time for decoding/initializing etc hope it will help someone

Upvotes: 0

Confused
Confused

Reputation: 6278

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

Upvotes: 2

Related Questions