更新:
在我的帖子中,我认为这是一个导致崩溃的physicsBody属性错误,但事实并非如此。
当两个物体发生碰撞时,它应该结束游戏,并显示游戏结束屏幕。但是,在随机时间,应用程序将崩溃。我找不到它为什么会这样做的模式。有时它是第一次这样做,其他的,我可以在它崩溃应用程序之前多次重启。我收到错误:
'Attemped to add a SKNode which already has a parent: <GameOverLayer> name:'(null)' position:{0, 0} accumulatedFrame:{{0, 0}, {320, 568}}'
有问题的代码是此方法的最后一行:
- (void) showGameOverLayer
{
//Remove currently exising on pillars from scene and purge them
for (int i = self.children.count - 1; i >= 0; i--)
{
SKNode* childNode = [self.children objectAtIndex:i];
if(childNode.physicsBody.categoryBitMask == pillerCategory)
{
[childNode removeAllActions];
}
}
[_flappyBird removeAllActions];
_flappyBird.physicsBody.velocity = CGVectorMake(0, 0);
self.physicsWorld.gravity = CGVectorMake(0, 0.0);
_flappyBird.hidden = YES;
_gameOver = YES;
_gameStarted = NO;
_dt = 0;
_lastUpdateTimeInterval = 0;
_lastSpawnTimeInterval = 0;
[_startGameLayer removeFromParent];
[self addChild:_gameOverLayer];
}
我正在学习使用SpriteKit,我只是按照制作像Flappy Bird这样的游戏的教程,所以这对我来说都是比较新的。
显示_gameOverLayer removeFromParent
部分的每个地方:
- (void) showStartGameLayer
{
//Remove currently exising on pillars from scene and purge them
for (int i = self.children.count - 1; i >= 0; i--)
{
SKNode* childNode = [self.children objectAtIndex:i];
if(childNode.physicsBody.categoryBitMask == pillerCategory)
{
[childNode removeFromParent];
}
}
//Move Flappy Bird node to center of the scene
self.flappyBird.position = CGPointMake(self.backgroundImageNode.size.width * 0.5f, self.frame.size.height * 0.6f);
[_gameOverLayer removeFromParent];
_flappyBird.hidden = NO;
[self flyingBird];
[self addChild:_startGameLayer];
}
- (void) startGame
{
_score = 0;
_gameStarted = YES;
[_startGameLayer removeFromParent];
[_gameOverLayer removeFromParent];
self.flappyBird.position = CGPointMake(self.backgroundImageNode.size.width * 0.3f, self.frame.size.height * 0.6f);
//To have Gravity effect on Bird so that bird flys down when not tapped
self.physicsWorld.gravity = CGVectorMake(0, -4.0);
}