【发布时间】:2014-07-30 13:32:17
【问题描述】:
我在 Xcode 中使用 SpriteKit 制作游戏,但在使用粒子时遇到了问题。
SKEmitterNode的初始化方法:
-(SKEmitterNode *)newExplosionEmitter {
NSString *explosionPath = [[NSBundle mainBundle] pathForResource:@"explosionH" ofType:@"sks"];
SKEmitterNode *explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:explosionPath];
return explosion;
}
我的这个方法是CPU1的AI,在update方法中调用。
-(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 结束后,我放了“[explosion removeAllChildren]”只是为了确保我的粒子将被删除,但不管有没有它,一个仍然留在内存中(我猜)并且仍在缓冲。 是因为我在场景中将它声明为静态 SKEmitterNode 吗?
提前致谢!
【问题讨论】:
-
你能显示 newExplosionEmitter 吗?因为在这种情况下,您似乎需要从父级中删除爆炸本身。不过不确定。
-
请将其编辑到您的第一篇文章中:) 因为您没有从可能是问题的父级中删除发射器。而不是 [explosion removeAllChildren] 尝试 [explosion removeFromParent]
-
在爆炸的 runAction 的序列中,最后一步是将其从父级中移除。这就是为什么我不明白为什么它不删除它。
-
你是对的。监督了那个。对不起!我一回家就测试一下。但我想早点会有答案:)
-
场景中还剩下什么?粒子还是发射器?
标签: ios objective-c sprite-kit particle-system skemitternode