游戏fps关闭

时间:2014-09-15 03:45:33

标签: ios ios7 sprite-kit

我为Game Over Scene创建了View Controller。当我从Game Over ViewController重启游戏时,fps正在关闭。第一次我的fps是30+,当我重新开始游戏时它会关闭到20,然后我又重新启动它关闭到10 ... 有时我看到这个错误:GameOverViewController的开始/结束外观转换的不平衡调用:0x9bb8490>。

ViewController.m

- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(goToGameOverViewController:)
 name:@"GoToGameOverViewController"
 object:nil];
}

-(void)goToGameOverViewController:(NSNotification *) notification {
GameOverViewController *gameOverController = [self.storyboard   instantiateViewControllerWithIdentifier:@"GameOverViewController"];

[self.navigationController pushViewController:gameOverController animated:NO];
 }
------------------------------------------------------------------------------

MyScene.m


- (void)gameOver {
[[NSNotificationCenter defaultCenter]
 postNotificationName:@"GoToGameOverViewController" object:self];
}

GameOverView.m


-(void)playPressed
{
GameViewController *gameController = [self.storyboard instantiateViewControllerWithIdentifier:@"GameViewController"];

[self.navigationController pushViewController:gameController animated:YES];
}

-(void)createPlayButton
{
UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:playBtn];

playBtn.frame = CGRectMake(100, 100, 200, 44);
[playBtn setTitle:@"Play Again" forState:UIControlStateNormal];
[playBtn sizeToFit];
[playBtn addTarget:self action:@selector(playPressed)  forControlEvents:UIControlEventTouchUpInside];
}

2 个答案:

答案 0 :(得分:0)

您的代码和方法存在几个核心问题:

  1. 您不断将UIViewController推送到UINavigationController。相反,也许在游戏结束后,您可以拨打popToRootViewControllerAnimated:或其他人。你推动堆栈的每个UIViewController都会永远存在。
  2. 您永远不会从NSNotificationCenter移除观察者。这意味着每次完成一个关卡时,每个viewController都会尝试推送一个GameOver viewController。这就是造成“不平衡......”警告的原因。如果您遵循我的#1建议,您只需要覆盖dealloc并调用以下代码来解决该问题。
  3. playPressed中使用此代码:

    GameViewController *gameController = [self.storyboard instantiateViewControllerWithIdentifier:@"GameViewController"];
    [self.navigationController setViewControllers:@[gameController] animated:YES];
    

    后来在GameViewController

    -(void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self]
    }
    

    这将确保您的ViewController以及您的场景被取消分配,并且您没有多个ViewController尝试同时推送游戏。

答案 1 :(得分:-1)

请确保从SKScene离开时ViewController释放:

-(void)goToGameOverViewController:(NSNotification *) notification {
    [((SKView *)self.view) presentScene:nil];

    GameOverViewController *gameOverController = [self.storyboard   instantiateViewControllerWithIdentifier:@"GameOverViewController"];

    [self.navigationController pushViewController:gameOverController animated:NO];
}