22

I currently have this code in a collide statement where if collide with object then this particle happens but how do I stop it? As it goes on forever whereas I only want to happen a couple of times per contactetc

SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle]     pathForResource:@"ff" ofType:@"sks"]];
emitter.zPosition = 0;
emitter.particlePositionRange = CGVectorMake(0, self.size.height);
emitter.position = CGPointMake(self.size.width, self.size.height/2);
[self addChild:emitter];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
John B
  • 105
  • 1
  • 2
  • 11

6 Answers6

21

When you use the particle-editor you can set the maximum number of particles to create. It's the field below "Particle Texture".The official description is:

"The maximum number of particles that the emitter creates over the emitter’s lifetime. After this number is reached, no more particles are created by the emitter. Enter 0 to remove particle limits."

Also see: Particle Emitter Editor Guide

Of course, you should remove the emitter-node from its parent after it created the maximum number of particles. This can be done by creating an action-sequence that waits for a few seconds and removes the emitter-node from its parent [SKAction removeFromParent].

Nino
  • 402
  • 3
  • 8
  • 1
    Wow so deceptively SIMPLE! thanks for that man feel a bit stupid now :) – John B Apr 07 '14 at 01:10
  • Is:- sparkEmmiter.particleLifetime = 0 worth full for that? – Raksha Saini Mar 29 '16 at 08:12
  • 2
    `This can be done by creating an action-sequence that waits for a few seconds and removes the emitter-node from its parent [SKAction removeFromParent].` waits a few seconds after the particle was created? Is there any trigger to run a sequence after particles have finished? – SSH This Sep 01 '16 at 21:32
  • @Raksha thats not actually correct, particleLifetime is the lifetime of each particle, to set the number of particles emitted to 50 you have to set: `sparkEmitter.numParticlesToEmit = 50` – SSH This Sep 01 '16 at 21:33
  • I know it's slightly off-topic, but what about making the emitter start? Do you set the birth rate to 0 and then when you want to make it start set it to what it needs to be to start emitting? – Lee Probert Jun 16 '17 at 20:20
  • @SSHThis did you find out if there's a trigger or callback when the particles are done emitting? – Crashalot Jun 22 '17 at 07:19
14

Use setParticleBirthRate:0 to stop emitting particles. This is the most realistic way to turn off an emitter.

If you want it to disappear immediately then use removeFromParent.

If using setParticleBirthRate remember the original value to turn it back on later. for instance.

@implementation GameScene
{    
    SKEmitterNode *rocketfire;
    float rocketfire_birthrate;
}

// ...
-(void)init {
    // ... 
    rocketfire_birthrate = rocketfire.particleBirthRate;
}

// turn on the emitter (thrust rocket) when the screen is touched
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [rocketfire setParticleBirthRate:rocketfire_birthrate];

}

// turn off the emitter on touches ended    
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [rocketfire setParticleBirthRate:0];
}
Patrick Collins
  • 4,046
  • 3
  • 26
  • 29
3

This is similar to the answer here provided by Tapir, with the exception that I don't use the selector. I'm not sure if that is required in previous versions but in ios 10 I don't see my node counts increasing and it does work. ie. add an emitter, pop a specified number of particles and then remove the emitter. In this case, I've modified it to add a single particle.

-(void) gainPoints:(int)x  y:(int)y {


NSString *pPlus1Path = [[NSBundle mainBundle] pathForResource:@"PlusEmitter" ofType:@"sks"];
SKEmitterNode *pEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:pPlus1Path];


// Place the emitter at the specified position
pEmitter.position = CGPointMake(x,y);
pEmitter.name = @"plus1";
pEmitter.particleLifetime = 1;
pEmitter.particleBirthRate = 1;
pEmitter.numParticlesToEmit = 1;
// Send the particles to the scene.
pEmitter.targetNode = self.scene;

[self addChild:pEmitter];

SKAction *pRemoveNode = [SKAction removeFromParent];
SKAction *pWaitForSecsN = [SKAction waitForDuration:1];
SKAction *pSequence = [SKAction sequence:@[pWaitForSecsN,pRemoveNode]];
[pEmitter runAction:pSequence];

}

Community
  • 1
  • 1
netskink
  • 4,033
  • 2
  • 34
  • 46
3

SWIFT 4:

I think I have a slightly better approach for turning it off:

1) Put the birth rate to 0:

pEmitter.particleBirthRate = 0

2) Run a delay action for the lifetime:

let waitAction = SKAction.wait(forDuration: pEmitter.particleLifetime)
pEmitter.run(waitAction, completion: {
    pEmitter.removeFromParent()
})

NOTE: Might not be 100% accurate if you have a range of partilcelifetime.

  • 1
    `let waitAction = SKAction.wait(forDuration: pEmitter.particleLifetime + (pEmitter.particleLifetimeRange/2.0))` – Tim Jul 19 '19 at 19:47
3

For those who want to freeze a particle emitter on a specific part of the animation, instead of removing it from screen, you can pause the emitter:

emitter.isPaused = true
jvarela
  • 3,744
  • 1
  • 22
  • 43
1

when pause you need :

emitter.hidden = true

when restart you need :

emitter.hidden = false
emitter.resetSimulation()
Tritmm
  • 1,972
  • 18
  • 14
  • SKEmitterNode does not support a hidden property. – boehmatron Apr 17 '17 at 17:10
  • @boehmatron sorry sir! are you sure about this?? SKEmitterNode inheritance from SKNode which have a hidden property. check and test!!! please!! – Tritmm Apr 18 '17 at 07:22
  • hey @Tritmm zou are right - sorry for that. I was trying the hidden property again in another separate project and it worked as described,... I will correct your rating as soon as possible. Sorry for that. This was not intended. – boehmatron Apr 18 '17 at 16:16
  • @boehmatron haha. no problem! this is your small mistake and i get a same mistake sometime. good day to you! – Tritmm Apr 19 '17 at 07:39