Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stop a particle effect? (SKEmitterNode)

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];
like image 433
John B Avatar asked Apr 06 '14 18:04

John B


3 Answers

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].

like image 129
Nino Avatar answered Nov 06 '22 01:11

Nino


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];
}
like image 13
Patrick Collins Avatar answered Nov 05 '22 23:11

Patrick Collins


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];

}

like image 3
netskink Avatar answered Nov 06 '22 00:11

netskink