2013-04-12 108 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]];