如何知道精灵效果何时完成

时间:2014-08-10 06:19:01

标签: objective-c ios7 sprite-kit

我在某些点上在SKScene中运行精灵效果。当效果未运行时,我想暂停场景,否则即使没有效果也会占用额外的CPU。我无法找到一个委托告诉我何时效果停止渲染,以便我可以再次暂停场景。我可以通过让它等待安全时间来强制它,但是有更优雅的方式来知道效果何时结束?

在viewWillAppear中设置:

self.skScene = [EffectsScene sceneWithSize:self.view.frame.size];
[self.skScene setBackgroundColor:[UIColor whiteColor]];
[self.skView presentScene:self.skScene];
[self.skView setPaused:YES];

在特定时间调用以运行效果的方法:

-(void)deleteEffect
{
    if (shouldRunDeleteEffect)
    {
        [self.skScene smokeEffectAtX:self.view.frame.size.width/2 andY:0];
        [self.skView setPaused:NO];
        shouldRunDeleteEffect = NO;
    }
}

实际场景细节:

-(void)smokeEffectAtX:(float)x andY:(float)y
{
    SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SmokeEffect" ofType:@"sks"]];
    emitter.position = CGPointMake(x,y);
    emitter.numParticlesToEmit = 1000;
    [self addChild:emitter];
}

1 个答案:

答案 0 :(得分:1)

此方法创建一个SKEmitterNode,根据其属性和持续时间参数计算发射器运行的时间,并在发射器完成运行后向发射器添加SKAction以将其从场景中移除。

- (void) newSmokeNodeAtPosition:(CGPoint)position withDuration:(NSTimeInterval)duration
{
    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.numParticlesToEmit = duration * emitter.particleBirthRate;

    // 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
    [emitter runAction:[SKAction sequence:@[[SKAction waitForDuration:totalTime],
                                            [SKAction removeFromParent]]]
            completion:^{
                // Add code here to run when emitter is done
                self.scene.paused = YES;
            }];
    [self addChild:emitter];
}