4

I have an SKShapeNode with a child SKEmitterNode. I have attached an SKAction sequence where the last action is removeFromParent. The node behaves correctly without the emitter, doing it's action then removing itself. However, if the emitter is attached then the whole program crashes (execution jumps to main method and appears to hang) when the shapenode is removed.

-(void)fireLasers
{
    SKShapeNode* laser1 = [[SKShapeNode alloc] init];
    //laser1 configuration removed for brevity

    NSString *laserParticlePath = [[NSBundle mainBundle] pathForResource:@"LaserParticle" ofType:@"sks"];
    SKEmitterNode *laserFire = [NSKeyedUnarchiver unarchiveObjectWithFile:laserParticlePath];
    [laser1 addChild:laserFire];

    SKAction* s1 = [SKAction moveByX:0 y:1000 duration:1.0];
    SKAction* s2 = [SKAction removeFromParent];

    SKAction* sequence = [SKAction sequence:@[s1, s2]];
    [laser1 runAction:sequence];

    [self.parent addChild:laser1];
}

The program will run in both of these cases:

  1. I do not attach the emitter
  2. I do not include the removeFromParent action

I'm guessing this will work if I attach an action to the emitter to removeFromParent (after say .9 seconds) before the shapenode gets removed, but that seems like a tedious long term solution.

Does anyone know what happens to a node with child nodes where removeFromParent is applied to the parent node, or how I can fix this issue?


Update based on LearnCocos2D's answer

In my full code, I'm actually creating one laser as above, then copying it to a second laser. I used LearnCocos2D's code from below, which works for one laser but fails for two. The important changes for my code, based on LearnCocos2D's answer is:

laserFire.name = @"laserfire";
SKAction* s2 = [SKAction runAction:[SKAction removeFromParent] onChildWithName:@"laserfire"];

Giving laserfire a name and removing it from parent based on that name works when cloning the laser for use when I want multiple lasers fired at once.

4

2 回答 2

3

我可以确认这会崩溃。我设计了一个可行的解决方案。如果在移除形状节点之前移除发射器,崩溃就会消失:

SKAction* s1 = [SKAction moveByX:0 y:1000 duration:1.0];
SKAction* s2 = [SKAction runBlock:^{
    [laserFire removeFromParent];
}];
SKAction* s3 = [SKAction removeFromParent];

SKAction* sequence = [SKAction sequence:@[s1, s2, s3]];

当 Apple 的错误报告重新上线时,我会将其报告为错误。我无法解释为什么这会崩溃。

于 2013-11-16T14:58:59.380 回答
1

当我有多个发射器连接到一个精灵时,我遇到了这个问题。我发现的技巧是在将每个发射器添加到父级后为其命名。似乎 SpriteKit 从父级中删除时必须在内部使用该名称。

于 2014-08-23T20:29:50.943 回答