iphoneioscaemitterlayercaemittercell

CAEmitterCell does not respect the duration value


I have an emitter that I want to end after 2 seconds, but I can't get it to stop running!

my code is as follows:

// Emitter Layer
    //
    emitter = [CAEmitterLayer layer];
    emitter.emitterSize = CGSizeMake(20, 20);
    emitter.renderMode = @"additive";
    emitter.emitterMode = @"points";
    emitter.emitterShape = @"rectangle";
    emitter.emitterPosition = CGPointMake(310, 230);
    //emitter.duration = 2; // setting duration on the emitterLayer prevents anything from firing it seems

// Emitter Cells
    //

    // 'New Emitter' cell configuration
    newEmitter = [CAEmitterCell emitterCell];
    newEmitter.scaleSpeed = 7;
    newEmitter.lifetime = 2.481715;
    //newEmitter.duration = 2; // I would expect this to stop the emitter after 2 seconds, but it doesn't!
    newEmitter.velocity = 332.3636968085106;
    newEmitter.contents = newemitterImg;
    newEmitter.name = @"New Emitter";
newEmitter.scaleRange = 4.178236607142859;
    newEmitter.lifetimeRange = 1.6;
    newEmitter.greenRange = -2.775558e-17;
    newEmitter.birthRate = 40;
    newEmitter.emissionRange = -6.283185306666667;
    newEmitter.scale = 0;

    // Emitter Cell Chains
    //

    emitter.emitterCells = [NSArray arrayWithObjects:newEmitter, nil];

There seems to be very little information on this available despite being a part of iOS for about 2 years!


Solution

  • You can do this with a timer, and a method to stop the emitter generating new particles...

    At the end of your existing code add:

        [newEmitter setName:@"newEmitter"];
        [NSTimer scheduledTimerWithTimeInterval:2.0
                                         target:self
                                       selector:@selector(stopEmitter:)
                                       userInfo:nil
                                        repeats:NO];
    

    Then make a stopEmitter: method…

        - (void) stopEmitter:(NSTimer*)timer
        {
            [emitter setValue:@0
                   forKeyPath:@"emitterCells.newEmitter.birthRate"];
        }