我试图在touchesBegan方法中添加粒子效果,因此当用户触摸绘制的精灵(SKSpriteNode)时,它会绘制粒子效果。但是,我收到错误 尝试添加已有父级的SKNode:SKEmitterNode 。添加一些上下文...游戏是宝石迷阵/糖果粉碎风格,其中基于相邻颜色删除块(deleteNode)。在触摸事件中,我以递归方式迭代,检查相邻块并将它们添加到数组中以便稍后删除。在删除每个块(deleteNode)之前,我希望发生粒子事件。他们都继承自SKNode(对吧?),所以我不理解这个冲突......
@interface
{
NSString *blockParticlePath;
SKEmitterNode *blockParticle;
}
在初始化方法中......
blockParticlePath = [[NSBundle mainBundle] pathForResource:@"blockParticle" ofType:@"sks";
blockParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:blockParticlePath];
in touchesBegan ...
blockParticle.position = deleteNode.position;
blockParticle.particleColor = deleteNode.color;
[self addChild:blockParticle];
为了确保我不会疯狂,我检查了其他论坛并看到了为场景添加粒子效果的相同逻辑。提前致谢。如果您需要更多信息,请告诉我。
答案 0 :(得分: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 stack。
我完全不理解,也许这可以帮助你进一步理解。