2

I am new to Spritekit. Just starting to make my 3rd-4th game. So I'd like to know what is the correct way to use SKEmitterNode programmatically.

In particular what is the correct practice if I want to have multiple particles in my game - jet engine fire, and also multiple explosions when bullet hits enemy ship.

Do I keep on creating new SKEmitterNode and adding it to the scene or do I add a single global EmitterNode and change its properties in the update function?

// FIRE ENGINE

SKEmitterNode *_myEmitter = [[SKEmitterNode alloc] init];
// codes..
[self addChild:_myEmitter]; // add to scene

// EXPLOSION 

SKEmitterNode *_myEmitter = [[SKEmitterNode alloc] init];
// codes..
[self addChild:_myEmitter]; // add to scene

OR

// IN INIT METHOD

_myEmitter = [[SKEmitterNode alloc] init]; // global variable
// codes..
[self addChild:_myEmitter]; // add once


// IN UPDATE

//when explode
_myEmitter.particleTexture = [SKTexture textureWithImageNamed:@"explode.png"];
... modify code


//when engine fire
_myEmitter.particleTexture = [SKTexture textureWithImageNamed:@"fire.png"];
... modify code

?

Cœur
  • 37,241
  • 25
  • 195
  • 267
GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • Like hamobi said, it depends on situation. If you have different types of effects, it is convenient to have different emitters defined instead to configure them programatically... – Whirlwind Mar 08 '16 at 11:52

1 Answers1

2

You can do it either way. In my game I had different emitter types for different types of effects. The reason for that is because each of my emitter's properties would be so different that it wouldnt make sense to generalize. The other reason is because I might need to have multiple emitters on the screen at once.

If you know that most or all of your properties are going to be the same and you're only going to use one emitter at a time then you can just change the texture.

Basically it depends on what youre trying to do. There isnt one best practice. I do recommend using the particle editor though. Just must faster to tweak things visually and you can always change the properties programmatically later on.

hamobi
  • 7,940
  • 4
  • 35
  • 64
  • Thanks for sharing. As for particle editor I use my own app to do that - Particle X. :D I am only worried about memory-wise. I guess with ARC, things just get removed automatically when emitter is done doing its job. – GeneCode Mar 09 '16 at 06:57
  • just remove the emitter from the scene if you no longer need it – hamobi Mar 09 '16 at 06:58
  • Can't i just use and ignore? esp emitter like explosion (one time non continuous)? Thought ARC will take care of that? – GeneCode Mar 09 '16 at 07:00
  • 1
    I wouldn't worry about it either way unless you're facing performance troubles. – hamobi Mar 09 '16 at 07:16
  • 1
    @Rocotilos I would consider removing emitters from the scene after emitting is done. As you said, you can have many explosions, but those emitters after the explosion effect is over, will remain in node tree and eat up resources (in some small percent I guess, but they will be still present). You can check this by observing nodes count (or by enumerating all nodes added to node tree). Read more here about how you can remove an emitter when emitting is done (there is no default completion handler for this though) http://stackoverflow.com/a/31731439/3402095 – Whirlwind Mar 09 '16 at 12:30
  • I agree with @hamobi about premature optimization, but still, removing the unused "dead" emitter nodes from a node tree might be a good move. – Whirlwind Mar 09 '16 at 12:31
  • 1
    @Rocotilos ARC will not dispose this nodes when emitting is finished because there is a reference to them (they are added to children array of another node, probably scene). For example, I will assume that emitters are added to the scene. They will be disposed when there is more reference to them (when the scene is released if memory management is correctly handled), but that is too late and they will eat up resources through the whole gameplay. – Whirlwind Mar 09 '16 at 12:58