2015-05-28 72 views
0

我正在使用SKEmitterNode發出一些水龍頭的粒子。我效仿了Periscope中的心靈。我如何輕鬆進/出SKEmitterNode.particleBirthRate

我發現,當我添加發射器時,它不像它只是「打開」。也就是說,粒子以遠離生成點的完整alpha值出現(就好像它們已經存活了一段時間)。就好像發射器一直在運行,它突然顯示出來。

我後面是點擊 - >添加節點 - >從點開始發射 - >發射一會兒 - >關閉 - >刪除節點。

我試着使用GCD調整particleBirthRate:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if(self.burstEmitter.parent){ 
     return; 
    } 

    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInNode:self]; 
    self.burstEmitter.position = point; 
    self.burstEmitter.particleBirthRate = 0; 
    [self addChild:self.burstEmitter]; 


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     self.burstEmitter.particleBirthRate = 10; 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      self.burstEmitter.particleBirthRate = 0; 

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
       [self removeAllChildren]; 
      }); 
     }); 
    }); 
} 

然後我雖然SKAction可能是合適的工具(它可能是...)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if(self.burstEmitter.parent){ 
     return; 
    } 

    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInNode:self]; 
    self.burstEmitter.position = point; 
    self.burstEmitter.particleBirthRate = 10; 
    [self addChild:self.burstEmitter]; 


    [self.burstEmitter runAction:[SKAction sequence:@[ 
                 [SKAction fadeInWithDuration:1], 
                 [SKAction waitForDuration:2], 
                 [SKAction fadeOutWithDuration:1], 
                 [SKAction removeFromParent] 
                 ]]]; 
} 

這明顯變淡發射節點不在「關閉」,

我打算使用CAEmitterLayer。我也嘗試過,但碰到同樣的問題。


更新:我發現了一個解決方案:resetSimulation。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if(self.burstEmitter.parent){ 
     return; 
    } 

    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInNode:self]; 
    self.burstEmitter.position = point; 
    self.burstEmitter.particleBirthRate = 10; 
    [self.burstEmitter resetSimulation]; 
    [self addChild:self.burstEmitter]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     self.burstEmitter.particleBirthRate = 0; 

     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      [self removeAllChildren]; 
     }); 
    }); 
} 

這完成了我以後的事情。如果可能的話,我仍然很好奇使用SKAction。

回答

1

您可以使用SKAction塊。有很多方法可以完成你所要求的。這裏是一個簡單的:

SKAction *wait0 = [SKAction waitForDuration:0.5]; // time in secs 
SKAction *block0 = [SKAction runBlock:^{ 
    self.burstEmitter.particleBirthRate = 9; 
}]; 
SKAction *block1 = [SKAction runBlock:^{ 
    self.burstEmitter.particleBirthRate = 8; 
}]; 
SKAction *block2 = [SKAction runBlock:^{ 
    self.burstEmitter.particleBirthRate = 7; 
}]; 

// and so on... 

[self runAction:[SKAction sequence:@[block0, wait0, block1, wait0, block2]]]; 
1

使用SKKeyframeSequence。這就是它的目的。

請參閱SKEmitterNode docs page上關於「使用關鍵幀序列爲粒子屬性配置自定義坡道」的討論。

+0

如果'particleBirthRate'屬性本身就是可動畫的,就像CAEmitterCell和CAEmitterLayer屬性一樣;但我沒有看到這方面的任何證據。 – matt