简体   繁体   English

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

[英]Is this the right way to start and stop a particle system

I'm currently practicing with particle systems and I was wondering if the following code is the right way to stop and start a particle when a button is tapped? 我目前正在练习粒子系统,我想知道以下代码是否是在点击按钮时停止和启动粒子的正确方法?

The code works fine, I touch the start button and the particle starts, I touch the stop button and the particle stops but I'm not sure if removeFromSuperLayer is the right method to use. 代码工作正常,我触摸开始按钮,粒子开始,我触摸停止按钮,粒子停止,但我不确定removeFromSuperLayer是否是正确的使用方法。 As I said, the code does what I need but I just want to make sure that the particle won't keep running in the background even after calling removeFromSuperLayer and end-up wasting resources. 正如我所说,代码完成了我的需要,但我只是想确保即使在调用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]; 
}

Thanks a lot 非常感谢

You could use a method in which you put the following: 您可以使用放置以下内容的方法:

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

With this you should be able to stop the emitting, without having to remove and re-create the layer each time when the start button is pressed. 使用此功能,您应该能够停止发射,而无需在每次按下开始按钮时移除并重新创建图层。

To start again, simply do: 要重新开始,只需:

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

Hope this helps. 希望这可以帮助。

It's funny but just modifying the birthRate like this self.emitterCell.birthRate = 0.0f; 这很有趣,但只是像这样修改birthRate self.emitterCell.birthRate = 0.0f; in a method doesn't stop the emitterCell, in fact it looks like if it appends instead of stopping it, in other words if I change it to self.emitterCell.birthRate = 100; 在一个方法中不会停止emitterCell,实际上看起来它是否附加而不是停止它,换句话说,如果我将它改为self.emitterCell.birthRate = 100; it adds 100 more particles to the existing particles. 它为现有的粒子增加了100多个粒子。 Lucky I found the solution. 幸运的我找到了解决方案。

I basically had to give my emitterCell a name emitterCell.name = @"_myCell"; 我基本上必须给我的emitterCell一个名字emitterCell.name = @"_myCell"; and then in my stop method modify it like this [emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:@"emitterCells._myCell.birthRate"]; 然后在我的stop方法中修改它像[emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:@"emitterCells._myCell.birthRate"]; and it worked. 它起作用了。

This is what I did that worked. 这就是我的工作。 This is assuming you already have an image in your project named myImage. 假设您的项目中已有一个名为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

Rather than change the birthRate of the each of the emitterCells you can change the birthrate of the emitterLayer itself. 您可以更改emitterLayer本身的出生率,而不是更改每个emitterCell的birthRate。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM