2013-04-12 110 views
4

我有一个CAEmitterCell,我已经设置了一个特定的颜色。该文件说,this property is animatable,我想为我的游戏中的不同玩家(所有人在开始时选择他们的颜色)在许多不同颜色之间进行动画处理。动画CAEmitterCell颜色属性

这是我EmitterCell,当我将它设置:

// 
    // Emitter Cells 
    // 

    // 'New Emitter' cell configuration 
    newEmitter = [CAEmitterCell emitterCell]; 
    newEmitter.scaleSpeed = 10; 
    newEmitter.lifetime = 2.481715; 
    newEmitter.velocity = 332.3636968085106; 
    newEmitter.contents = newemitterImg; 
    newEmitter.name = @"New Emitter"; 
    newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor]; 
    newEmitter.scaleRange = 4.178236607142859; 
    newEmitter.lifetimeRange = 1.6; 
    newEmitter.greenRange = -2.775558e-17; 
    newEmitter.birthRate = 40; 
    newEmitter.emissionRange = -6.283185306666667; 
    newEmitter.scale = 0; 

    // 
    // Emitter Cell Chains 
    // 
    emitter.emitterCells = [NSArray arrayWithObjects:newEmitter, nil]; 

而且这里是我在这种情况下,两种不同颜色之间只是弹跳测试颜色变化:

-(void)changeColor { 

    if (color == 0) { 
     color = 1; 
     NSLog(@"color = 1"); 

     [UIView animateWithDuration:1.5 delay:0 options:0 animations:^{ 
      newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor]; 
     } completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}]; 
    } else { 
     color = 0; 
     NSLog(@"color = 0"); 
     [UIView animateWithDuration:1.5 delay:0 options:0 animations:^{ 
      newEmitter.color = [[UIColor colorWithRed:1.00 green:0.50 blue:0.10 alpha:1.00] CGColor]; 
     } completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}]; 
    } 

} 

然而,当我运行这个时,颜色永远不会改变。我是否误解了“Animatable”的性质,还是仅仅需要针对CAEmitterCell以不同的方式去做?

回答

-1

您可以使用这些属性为发射器颜色设置动画。

newEmitter.redSpeed = 1.0; 
newEmitter.greenSpeed = 1.0; 
newEmitter.blueSpeed = 1.0; 
1

CAEmitterCells实际上是不同的。为了获得对他们的工作的动画,你需要采取下列步骤操作:

1.Assign一个名字你CAEmitterCell,如:

newEmitter.name = @"fire";

2.Access动画属性为这个发射器通过CAEmitterLayer例如:

//Set first before doing CABasicAnimation so it sticks 
newEmitter.redSpeed = 1.0; 

//Access the property with this key path format: @"emitterCells.<name>.<property>" 
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.fire.redSpeed"]; 
anim.fromValue = @(0.0); 
anim.toValue = @(1.0); 
anim.duration = 1.5; 
anim.fillMode = kCAFillModeForwards; 
[emitter addAnimation:anim forKey:@"emitterAnim"]; 
+0

如何ÿ你改变颜色?您的代码似乎不适用于更改颜色。 – Bobby

0

试试这个:

[newEmitter.emitterCells[0] setColor:[[UIColor yellowColor] CGColor]];