2014-04-19 23 views
2

我目前正在使用粒子系统进行练习,我在想如果下面的代码是正确的方式来停止并启动一个粒子,当一个按钮被点击时?这是开始和停止粒子系统的正确方式

该代码工作正常,我触摸开始按钮和粒子启动,我触摸停止按钮和粒子停止,但我不知道如果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]; 
} 

非常感谢

回答

2

你可以使用你把下面的方法:

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

有了这个,你应该能够停止发光,无需拆卸和重新 - 每次按下开始按钮时创建图层。

要重新开始,只需做:

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

希望这有助于。

+1

非常感谢,它比我想象的要多得多。 –

+0

@fs_tigre:'self.emitterCell.birthRate = 0.0f;'会隐藏图层吗? –

+0

不,看不到我的最新评论,看看我的最终解决方案。 –

2

这很有趣,但只是在一个方法中修改birthRate像这样self.emitterCell.birthRate = 0.0f;并不会停止emitterCell,实际上它看起来像是如果附加它而不是停止它,换句话说,如果我将其更改为self.emitterCell.birthRate = 100;它会增加100个更多颗粒到现有的颗粒。幸运我找到了解决方案。

我基本上不得不给我的emitterCell一个名字emitterCell.name = @"_myCell";,然后在我的停止方法修改它像这样[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