这是启动和停止粒子系统的正确方法吗



我目前正在练习粒子系统,我想知道当点击按钮时,以下代码是否是停止和启动粒子的正确方法?

代码运行良好,我触摸开始按钮,粒子开始,我触摸停止按钮,粒子停止,但我不确定removeFromSuperLayer是否是正确的使用方法。正如我所说,代码完成了我所需要的,但我只想确保粒子即使在调用removeFromSuperLayer后也不会继续在后台运行,从而浪费资源。

- (IBAction)stopAnimation:(id)sender
{
    [emitterLayer removeFromSuperlayer];
}
- (IBAction)startAnimation:(id)sender
{
    [self particle];
}
-(void) particle
{
    emitterLayer = [CAEmitterLayer layer]; 
    emitterLayer.emitterPosition = CGPointMake(50 ,50); 
    emitterLayer.emitterZPosition = 10; 
    emitterLayer.emitterSize = CGSizeMake(10,10); 
    emitterLayer.emitterShape = kCAEmitterLayerSphere; 
    CAEmitterCell *emitterCell = [CAEmitterCell emitterCell]; 
    emitterCell.scale = 0.1; 
    emitterCell.scaleRange = 0.2; 
    emitterCell.emissionRange = (CGFloat)M_PI_2; 
    emitterCell.lifetime = 10; 
    emitterCell.birthRate = 5; 
    emitterCell.velocity = 20; 
    emitterCell.velocityRange = 50; 
    emitterCell.yAcceleration = 0; 
    emitterCell.contents = (id)[[UIImage imageNamed:@"particleImage.png"] CGImage]; 
    emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell]; 
    [self.view.layer addSublayer:emitterLayer]; 
}

非常感谢

您可以使用一种方法,其中放入以下内容:

- (void)stopEmitting
{   
    self.emitterCell.birthRate = 0.0f;
}

这样,您就可以停止发射,而不必每次按下开始按钮时都删除并重新创建层。

要重新开始,只需执行:

- (void)startEmitting
{
    self.emitterCell.birthRate = <VAlUE HERE (greater than 0)>;
}

希望这能有所帮助。

您可以更改emitterLayer本身的出生率,而不是更改每个emitterCells的出生率。

- (void)stopEmitting
{   
    self.emitterLayer.birthRate = 0;
}
- (void)startEmitting
{
    self.emitterLayer.birthRate = 1;
}

这很有趣,但仅仅在方法中像self.emitterCell.birthRate = 0.0f;这样修改birthRate并不能停止emitterCell,事实上,如果它附加而不是停止,换句话说,如果我将其更改为self.emitterCell.birthRate = 100;,它会在现有粒子上再添加100个粒子。幸运的是,我找到了解决方案。

我基本上必须给我的emitterCell起一个名字emitterCell.name = @"_myCell";,然后在我的stop方法中像[emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:@"emitterCells._myCell.birthRate"];一样修改它,它就起作用了。

这就是我所做的工作。这是假设您的项目中已经有一个名为myImage的图像。

#import "SpriteViewController.h"
@implementation SpriteViewController
CAEmitterLayer *emitterLayer;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)stopAnimation:(id)sender
{
    [emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:@"emitterCells._myCell.birthRate"]; // new code
}
- (IBAction)startAnimation:(id)sender
{
    [self particle];
}
-(void) particle
{
    emitterLayer = [CAEmitterLayer layer];
    emitterLayer.emitterPosition = CGPointMake(50 ,50);
    emitterLayer.emitterZPosition = 10;
    emitterLayer.emitterSize = CGSizeMake(10,10);
    emitterLayer.emitterShape = kCAEmitterLayerSphere;
    CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
    emitterCell.name = @"_myCell";// new code
    emitterCell.scale = 0.1;
    emitterCell.scaleRange = 0.2;
    emitterCell.emissionRange = (CGFloat)M_PI_2;
    emitterCell.lifetime = 10;
    emitterCell.birthRate = 5;
    emitterCell.velocity = 20;
    emitterCell.velocityRange = 50;
    emitterCell.yAcceleration = 0;
    emitterCell.contents = (id)[[UIImage imageNamed:@"myImage.png"] CGImage];
    emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];
    [self.view.layer addSublayer:emitterLayer];
}
@end

最新更新