0

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];
}
sangony
  • 11,636
  • 4
  • 39
  • 55
C6Silver
  • 3,127
  • 2
  • 21
  • 49

2 Answers2

0

After you [self addChild:emitter]; add this code: [emitter resetSimulation];

sangony
  • 11,636
  • 4
  • 39
  • 55
0

It turns out the issue was with the call. I was focused on the pause issue so the call portion didn't seem the problem. However, the call to the class was in a loop as this effect could happen on x objects. Being in a loop caused the pauses to "step" on each other. The answer was to send an array of locations to the effects class above. Within that class I looped over the effects and only issue the pause when the last object effect was rendered.

C6Silver
  • 3,127
  • 2
  • 21
  • 49