I have an effect created with the code below. I have wired up a button that will fire this as a test. On first press the effect works as intended. However, on all subsequent requests the effect is never displayed. I have determined, via breakpoints, that the effect is being called and on both the first and subsequent runs it is making in into the completion handler. I have also checked the "totalTime" variable as part of the runAction and have verified that it is the same (2 seconds) for both the first run and subsequent runs. The interesting thing in that for the first run I do observe a several second delay between the preceding if statement and the break point at the completion handler. For all subsequent runs, however, there is virtually no delay between the preceding if and the completion handler. Again I have verified that the duration is 2 seconds on all runs. Lastly, if I comment out the [self.view setPaused:YES]
in the completion handler, the effect will run every time. Again though, and by the code below, you can see that the scene is unpaused prior. I am pausing the scenes this way as this is an occasional effect and I don't want to use extra CPU and thus battery for the majority of the time when these effects are not being generated.
- (void) smokeEffectAtPosition:(CGPoint)position withDuration:(NSTimeInterval)duration andView:(SKView *)view andPosVector:(CGVector)vector
{
SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SmokeEffect" ofType:@"sks"]];
emitter.position = position;
// Calculate the number of particles we need to generate based on the duration
emitter.particleBirthRate = 100;
emitter.numParticlesToEmit = duration * emitter.particleBirthRate;
emitter.particleLifetime = duration;
emitter.particlePositionRange = vector;
// Determine the total time needed to run this effect.
NSTimeInterval totalTime = duration + emitter.particleLifetime + emitter.particleLifetimeRange/2;
// Run action to remove the emitter from the scene
if ([self.view isPaused])
[self.view setPaused:NO];
[emitter runAction:[SKAction sequence:@[[SKAction waitForDuration:totalTime],
[SKAction removeFromParent]]]
completion:^{
if (![self.view isPaused])
[self.view setPaused:YES];
}];
[self addChild:emitter];
}