How do you stop a particle effect? (SKEmitterNode)
Asked Answered
M

6

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];
Mungovan answered 6/4, 2014 at 18:15 Comment(0)
C
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].

Celebrity answered 6/4, 2014 at 18:27 Comment(6)
Wow so deceptively SIMPLE! thanks for that man feel a bit stupid now :)Mungovan
Is:- sparkEmmiter.particleLifetime = 0 worth full for that?Unreserve
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?Gladi
@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 = 50Gladi
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?Outoftheway
@SSHThis did you find out if there's a trigger or callback when the particles are done emitting?Sphinx
A
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];
}
Alda answered 8/12, 2014 at 20:11 Comment(0)
Z
4

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.

Zosi answered 24/11, 2017 at 23:45 Comment(1)
let waitAction = SKAction.wait(forDuration: pEmitter.particleLifetime + (pEmitter.particleLifetimeRange/2.0))Assentor
E
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];

}

Explorer answered 18/12, 2016 at 18:33 Comment(0)
S
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
Stipulation answered 6/8, 2019 at 11:17 Comment(0)
T
1

when pause you need :

emitter.hidden = true

when restart you need :

emitter.hidden = false
emitter.resetSimulation()
Townsman answered 12/7, 2016 at 10:52 Comment(4)
SKEmitterNode does not support a hidden property.Stochmal
@Stochmal sorry sir! are you sure about this?? SKEmitterNode inheritance from SKNode which have a hidden property. check and test!!! please!!Townsman
hey @Townsman 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.Stochmal
@Stochmal haha. no problem! this is your small mistake and i get a same mistake sometime. good day to you!Townsman

© 2022 - 2024 — McMap. All rights reserved.