Objective-c SpriteKit 粒子不会删除操作完成后

标签: ios Objective-C
发布时间: 2016/10/2 11:33:34
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在做一个游戏在 Xcode,与 SpriteKit 和我工作与粒子时遇到的问题。

初始化 SKEmitterNode 的方法︰

-(SKEmitterNode *)newExplosionEmitter {
NSString *explosionPath = [[NSBundle mainBundle] pathForResource:@"explosionH" ofType:@"sks"];
SKEmitterNode *explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:explosionPath];
return explosion;

}

此方法我是 AI 的 CPU1,而被称为更新方法。

-(void)CPU1AI {
SKSpriteNode *CPU1 = (SKSpriteNode *)[self childNodeWithName:CPU1CategoryName];

int aiX = CPU1.position.x;
int aiY = CPU1.position.y;
int ballX = self.ball.position.x;
int ballY = self.ball.position.y;

if ((aiY-ballY)<250 && !CPU1isDead) {
    //CPU1 AI

}
if (CPU1isDead) {
    float posY = CPU1.position.y;
    float centerX = self.frame.size.width/2;
    CGPoint goToPos = CGPointMake(centerX, posY);

    SKAction *moveToPoint = [SKAction moveTo:goToPos duration:3];
    SKAction *removeNode = [SKAction removeFromParent];
    SKAction *CPUdead = [SKAction runBlock:^{CPU1isDead = NO;}];
    SKAction *sequence = [SKAction sequence:@[moveToPoint, removeNode, CPUdead]];
    explosion.position = CPU1.position;
    explosion.zPosition = 10;
    [CPU1 runAction:sequence];
    if (![explosion hasActions]) {
        explosion = [self newExplosionEmitter];
        [self addChild:explosion];
        [explosion runAction:[SKAction sequence:@[[SKAction fadeAlphaTo:0.5 duration:3.5],[SKAction removeFromParent]]]];
    }
}
[explosion removeAllChildren];
}

SK runActions 结束后,我把"[爆炸 removeAllChildren]"只是为了确保我的粒子将被删除,但不管有没有它,一个是仍留在内存 (我猜),仍然缓冲。 这是因为我在我的场景声明为静态的 SKEmitterNode 它吗?

在此先感谢 !

解决方法 1:

将这些方法添加到您 SKScene 子类 (来自苹果公司的网站,我稍微修改一下代码)......

- (SKEmitterNode*) newExplosionNode: (CFTimeInterval) explosionDuration
{
    SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"explosionH" ofType:@"sks"]];

    // Explosions always place their particles into the scene.
    emitter.targetNode = self;

    // Stop spawning particles after enough have been spawned.
    emitter.numParticlesToEmit = explosionDuration * emitter.particleBirthRate;

    // Calculate a time value that allows all the spawned particles to die. After this, the emitter node can be removed.

    CFTimeInterval totalTime = explosionDuration + emitter.particleLifetime+emitter.particleLifetimeRange/2;
    [emitter runAction:[SKAction sequence:@[[SKAction fadeAlphaTo:0.5 duration:totalTime],
                                            [SKAction removeFromParent]]]];
    return emitter;
}

- (void)createExplosionAtPosition:(CGPoint)position
{
    SKEmitterNode *explosion = [self newExplosionNode:3.5];
    explosion.position = position;
    explosion.zPosition = 10;
    [self addChild:explosion];
}

从代码中移除以下......

explosion.position = CPU1.position;
explosion.zPosition = 10;
[CPU1 runAction:sequence];
if (![explosion hasActions]) {
    explosion = [self newExplosionEmitter];
    [self addChild:explosion];
    [explosion runAction:[SKAction sequence:@[[SKAction fadeAlphaTo:0.5 duration:3.5],[SKAction removeFromParent]]]];
}

并将替换这

[self createExplosionAtPosition:CPU1.position];

我怀疑你要设置 CPU1isDead = NO if 语句,所以代码不执行多次。你还应删除 [爆炸 removeAllChildren]。

赞助商