我用下面的代码创建了一个效果。我已经连接了一个按钮,它将作为测试触发。在第一次按下时,效果按预期工作。但是,在所有后续请求中,效果永远不会显示。我已经通过断点确定效果正在被调用,并且在第一次和后续运行中它都进入了完成处理程序。我还检查了作为 runAction 的一部分的“totalTime”变量,并验证了它对于第一次运行和后续运行都是相同的(2 秒)。有趣的是,对于第一次运行,我确实观察到在前面的 if 语句和完成处理程序的断点之间有几秒钟的延迟。然而,对于所有后续运行,在前面的 if 和完成处理程序之间几乎没有延迟。我再次验证了所有运行的持续时间为 2 秒。最后,如果我注释掉[self.view setPaused:YES]
在完成处理程序中,效果每次都会运行。尽管如此,通过下面的代码,您可以看到场景之前没有暂停。我以这种方式暂停场景,因为这是一种偶尔的效果,并且我不想使用额外的 CPU 并因此在大多数情况下没有生成这些效果时使用电池。
- (void) smokeEffectAtPosition:(CGPoint)position withDuration:(NSTimeInterval)duration andView:(SKView *)view andPosVector:(CGVector)vector
{
SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SmokeEffect" ofType:@"sks"]];
emitter.position = position;
// Calculate the number of particles we need to generate based on the duration
emitter.particleBirthRate = 100;
emitter.numParticlesToEmit = duration * emitter.particleBirthRate;
emitter.particleLifetime = duration;
emitter.particlePositionRange = vector;
// Determine the total time needed to run this effect.
NSTimeInterval totalTime = duration + emitter.particleLifetime + emitter.particleLifetimeRange/2;
// Run action to remove the emitter from the scene
if ([self.view isPaused])
[self.view setPaused:NO];
[emitter runAction:[SKAction sequence:@[[SKAction waitForDuration:totalTime],
[SKAction removeFromParent]]]
completion:^{
if (![self.view isPaused])
[self.view setPaused:YES];
}];
[self addChild:emitter];
}