0

I have added some fireworks to an app. The emitters work a few times, then stops displaying.

Here is the Objective C code

in the .m

- (void) viewDidLoad {
 
  [super viewDidLoad];


  [self setupEmitter];
  [self setupFireWorkEmitterCells];

 }



-(void) setupEmitter{
emitterLayer = [CAEmitterLayer layer];
emitterLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-100);
emitterLayer.position = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

emitterLayer.emitterShape = kCAEmitterLayerPoint;

emitterLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.2 blue:0.2 alpha:0.500] CGColor];

emitterLayer.emitterPosition =  CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height);

emitterLayer.emitterSize = CGSizeMake(30, 30);

emitterLayer.emitterMode = kCAEmitterLayerOutline;
emitterLayer.renderMode = kCAEmitterLayerAdditive;

emitterLayer.needsDisplayOnBoundsChange = YES;
emitterLayer.delegate = self;

[self.view.layer insertSublayer:emitterLayer below:BottomBar.layer];

}



-(void) setupFireWorkEmitterCells{


 NSLog(@"setupFirework cellls");

 //============== EMITTER CELL 1 - Parent ==============

 // emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Sparks" ofType:@"sks"]];

//Create Cell
 CAEmitterCell *cell1 = [CAEmitterCell emitterCell];

 cell1.name = @"Parent";

 cell1.birthRate = 0.0;
 cell1.lifetime = 2.5;
 cell1.lifetimeRange = 0.0;
 cell1.beginTime = CACurrentMediaTime();
 cell1.velocity = 300.0;
 cell1.velocityRange = 100.0;
 cell1.xAcceleration = 0;
 cell1.yAcceleration = -100.0;
 cell1.emissionLatitude = 0.0 * (M_PI / 180.0);
 cell1.emissionLongitude = -90.0 * (M_PI / 180.0);
 cell1.emissionRange = 45.0 * (M_PI / 180.0);

 cell1.spin = 0.0 * (M_PI / 180.0);
 cell1.spinRange = 0.0 * (M_PI / 180.0);

 cell1.scale = 0.0;

 cell1.scaleRange = 0.0;
 cell1.scaleSpeed = 0.0;
 cell1.alphaRange = 0.0;
 cell1.alphaSpeed = 0.0;
 cell1.color = [UIColor colorWithRed:255.0/255.0 green: 255.0/255.0 blue: 255.0/255.0 alpha: 1.0].CGColor;
 cell1.redRange = 0.9;
 cell1.greenRange = 0.9;
 cell1.blueRange = 0.9;





 //============== EMITTER subCELL 1 - Trail ==============

 //  UIImage *image1_1 =   [UIImage imageNamed:@"Spark"];//.CGImage;

 CAEmitterCell *subcell1_1 = [CAEmitterCell emitterCell];

subcell1_1.contents = (id) [[UIImage imageNamed:@"FW-white.png"] CGImage];
subcell1_1.name = @"Trail";
subcell1_1.birthRate = 145.0;
subcell1_1.lifetime = 0.5;
subcell1_1.beginTime = 0.01;
subcell1_1.duration = 1.7;
subcell1_1.velocity = 80.0;
subcell1_1.velocityRange = 100.0;
subcell1_1.xAcceleration = 100.0;
subcell1_1.yAcceleration = 350.0;
subcell1_1.emissionLatitude = 0.0 * (M_PI / 180.0);
subcell1_1.emissionLongitude = -360.0 * (M_PI / 180.0);
subcell1_1.emissionRange = 22.5 * (M_PI / 180.0);
subcell1_1.spin = 0.0 * (M_PI/ 180.0);
subcell1_1.spinRange = 0.0 * (M_PI / 180.0);
subcell1_1.scale = 0.5;
subcell1_1.scaleRange = 0.0;

subcell1_1.scaleSpeed = 0.13;
subcell1_1.alphaRange = 0.0;
subcell1_1.alphaSpeed = -0.7;
subcell1_1.color = [UIColor colorWithRed:255.0/255.0 green: 255.0/255.0 blue: 255.0/255.0 alpha: 1.0].CGColor;
subcell1_1.redRange = 0.0;
subcell1_1.blueRange = 0.0;
subcell1_1.greenRange = 0.0;

    
//============== EMITTER sebCELL 2 - Fire Work ==============

CAEmitterCell *subcell1_2 = [CAEmitterCell emitterCell];


subcell1_2.contents = (id) [[UIImage imageNamed:@"FW-white.png"] CGImage];
subcell1_2.name = @"Firework";
subcell1_2.birthRate = 20000.0;
subcell1_2.lifetime = 15.0;
subcell1_2.lifetimeRange = 0.0;
subcell1_2.beginTime = 1.6;
subcell1_2.duration = 0.1;
subcell1_2.velocity = 190.0;
subcell1_2.velocityRange = 0.0;
subcell1_2.xAcceleration = 0.0;
subcell1_2.yAcceleration = 80.0;
subcell1_2.emissionLatitude = 0.0 * (M_PI / 180.0);
subcell1_2.emissionLongitude = 0.0 * (M_PI / 180.0);
subcell1_2.emissionRange = 360.0 * (M_PI / 180.0);
subcell1_2.spin = 114.6 * (M_PI / 180.0);
subcell1_2.spinRange = 0.0 * (M_PI / 180.0);
subcell1_2.scale = 0.1;
subcell1_2.scaleRange = 0.0;
subcell1_2.scaleSpeed = 0.12;// was.09
subcell1_2.alphaRange = 0.40;
subcell1_2.alphaSpeed = -0.7;
subcell1_2.color = [UIColor colorWithRed:255.0/255.0 green: 255.0/255.0 blue: 255.0/255.0 alpha: 1.0].CGColor;
subcell1_2.redRange = 0.0;
subcell1_2.greenRange = 0.0;
subcell1_2.blueRange = 0.0;

 cell1.emitterCells =  [NSArray arrayWithObjects:subcell1_1, subcell1_2, nil];

emitterLayer.emitterCells = [NSArray arrayWithObject:cell1];


}

The code I use to trigger the effect is

I connect the action below to a button:

 -(IBAction)StartFW {

 [emitterLayer setValue:[NSNumber numberWithInt:1] forKeyPath:@"emitterCells.Parent.birthRate"];

  }

To code I use to stop the effect is

I connect the action below to a button:

 -(IBAction)StopFW {

  [emitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.Parent.birthRate"];

  }

When I trigger the code, by pressing a button, the fireworks work fine, but after a little bit, they will not work. What can I try to solve this?

jsdigital
  • 21
  • 2
  • I noticed that if I take out either subcell1_1 or subcell1_2 from the emitterCells Array, the problem goes away. I only encounter the problem where the emitter stops working when both subcell1_1 and subcell1_2 are in the emitterCells Array. Anyone have any ideas? – jsdigital Jun 08 '23 at 19:27
  • Please provide a [mcve] and also explain what "they will not work" means. – matt Jun 10 '23 at 12:28
  • The animation displays fine for a short time, then it does not display at all. This behavior only seems to happen when I combine both subcell1_1 AND subcell1_2 in the emitterCells array. If I use only one of the subcells, then the animation displays all the time as it is supposed to when it is called. I am not sure why the odd behavior is occurring when both subcell1_1 AND subcell1_2 are combined. – jsdigital Jun 11 '23 at 14:44
  • The minimal reproducible example is the above code. The functions "setupEmitter" and "setupFireWorkEmitterCells" setup the animation. The other two snippets of code "StartFW" and "StopFW" start and stop the animation. – jsdigital Jun 11 '23 at 14:53
  • "The minimal reproducible example is the above code" So you claim that the code you've provided is sufficient for me to run the app and see the issue, without having to make any further guesses as to what you are doing in your app to summon the animation? Because I claim that it isn't. – matt Jun 11 '23 at 15:31
  • Sorry, I added the viewDidLoad that calls the setup of the emitterlayer and cells. the two other functions, Start & StopFW are conntected to a button to trigger each function start and stop. Hope this helps. Let me know if you need anything else. – jsdigital Jun 12 '23 at 16:22

0 Answers0