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];
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].
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];
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With