0

我正在尝试在 touchesBegan 方法中添加粒子效果,因此当用户触摸绘制的精灵(SKSpriteNode)时,它会绘制粒子效果。但是,我收到一个错误Attempted to add a SKNode which has a parent: SKEmitterNode。添加一些上下文...游戏是宝石/糖果迷恋风格,其中根据相邻颜色删除块(deleteNode)。在触摸事件中,我递归地迭代,检查相邻块并将它们添加到数组中以便稍后删除。在删除每个块(deleteNode)之前,我希望粒子事件发生。他们都继承自 SKNode (对吗?),所以我不明白冲突......

@interface
{
   NSString *blockParticlePath;
   SKEmitterNode *blockParticle;
}

在初始化方法中...

blockParticlePath = [[NSBundle mainBundle] pathForResource:@"blockParticle" ofType:@"sks";
blockParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:blockParticlePath];

在接触开始...

blockParticle.position = deleteNode.position;
blockParticle.particleColor = deleteNode.color;
[self addChild:blockParticle];

为了确保我没有发疯,我查看了其他论坛并看到了将粒子效果添加到场景中的相同逻辑。提前致谢。如果您需要更多信息,请告诉我。

4

1 回答 1

0

@whfissler,您的解释对查明此解决方案有很大帮助。

仅当我有许多 SKSpriteNodes 处于活动状态(气球游戏)时才会发生此错误。在每个气球上单击它都会弹出并添加一个 SKEmitterNode(爆炸)。似乎当爆炸产生的粒子相互接触时,我收到了和你一样的错误。我改变了我的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    SKEmitterNode *explosion = [SKEmitterNode orb_emitterNamed:@"ballonEksplosion"];
    CGPoint positionInScene = [touch locationInNode:self];
    explosion.position = positionInScene;
    SKSpriteNode *touchedSprite;
    for ( int i = 0; i < [[self nodesAtPoint:positionInScene] count]; i++)
    {
        touchedSprite = (SKSpriteNode *)[[self nodesAtPoint:positionInScene] objectAtIndex:i];
        if ([touchedSprite.name isEqualToString:@"BALLON"])
        {


            [(MBDBallon *)touchedSprite popAndRemoveWithSoundEffect];
            [self addChild:explosion];
        }
    }
}

至:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedSprite;
    for ( int i = 0; i < [[self nodesAtPoint:positionInScene] count]; i++)
    {
        touchedSprite = (SKSpriteNode *)[[self nodesAtPoint:positionInScene] objectAtIndex:i];
        if ([touchedSprite.name isEqualToString:@"BALLON"])
        {
            SKEmitterNode *explosion = [SKEmitterNode orb_emitterNamed:@"ballonEksplosion"];
            explosion.position = positionInScene;
            [(MBDBallon *)touchedSprite popAndRemoveWithSoundEffect];
            [self addChild:explosion];
        }
    }
}

它奏效了。对我来说,似乎我的爆炸 SKEmitterNode 在 SKScene 上一直保持很长时间,因此为 currentPosition 添加另一个 SKEmitterNode 会导致以下问题:

self nodesAtPoint:positionInScene

nodesAtPoint 堆栈。

我不完全理解它,也许这有助于你进一步理解。

于 2014-04-18T09:53:44.527 回答