6

When I first set up the emitter I can do this:

self.cell = [CAEmitterCell emitterCell];
self.cell.yAcceleration = 20;
...
self.emitter.emitterCells = [NSArray arrayWithObjects:self.cell,nil];

But say I create a timer that fires 5 seconds later, and I do this:

- (void)timerFired
{
    self.cell.yAcceleration = -10;

}

The timer fires, but the yAcceleration of the CAEmitterCell does not get changed. Or at least nothing changes in the particle emission on screen. How can I get a CAEmitterCell to respect changes I make to its properties?

soleil
  • 12,133
  • 33
  • 112
  • 183

2 Answers2

7

This is not real obvious, but here's the solution:

[self.emitter setValue:[NSNumber numberWithFloat:-10.0]
               forKeyPath:@"emitterCells.cell.yAcceleration"];

Where "cell" is the name given here:

[self.cell setName:@"cell"];
soleil
  • 12,133
  • 33
  • 112
  • 183
  • I'm confused. Where does the [self.cell setName:@"cell"] line go, and where is it referenced above? Do you mean to say that it should be [self.cell setName:@"NAME"]; and then [self.emitter setValue:... forKeyPath:@"emitterCells.NAME.yAcceleration"]; The answer as it is written is confusing me because cell is such a generic term I wonder if you are modifying the key path? – Erika Electra Mar 05 '14 at 09:21
  • @Cindeselia the later part is just to clarify that the cell name in their case was cell. If you had assigned yours the name "foo" then the keyPath would be "emitterCells.foo.yAcceleration". – Warpling Dec 07 '17 at 04:44
0

When you init the self.emitter with a new cell,the object will be retained,so ..when you change the cell.yAcceleration with a timer,the cell of self.emitter can not be changed,self.cell.yAcceleration has been changed already.So, you should use the key path of self.emitter.

Gwenc37
  • 2,064
  • 7
  • 18
  • 22