3

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?

Anton Shevtsov
  • 190
  • 1
  • 11
  • Particles in SpriteKit could be useful to adding simple effects to a node, and you should add few of them 'cause the engine sucks, is not able to handle a lot of them. Insted of partcles, you should think to add an animation composed by few sprites of an explosion (for example..): you'll have a better result both in speed and graphic quality. – Alessandro Ornano Mar 15 '18 at 16:03

2 Answers2

2

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

Confused
  • 6,048
  • 6
  • 34
  • 75
  • So if i use explosion i need to add it and pause it. When i need to reuse it i just reset it. That is understandable. But how do i approach when i need many particles for ex. when i have bullet vs player collision. I am adding them on contact point and there are many bullets colliding at very short period of time. Any hint? – Anton Shevtsov Mar 15 '18 at 14:18
0

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

Anton Shevtsov
  • 190
  • 1
  • 11