0

我有 3 个视图控制器。VC1(初始和主屏幕)、VC2(SKScene 和游戏)、VC3(UIViewController 和游戏结束屏幕)。我的问题是当我从 VC1 开始,然后转到 VC2,然后移动到 VC3,然后回到 VC1,它们都堆叠在一起,所以看起来像这样。VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3,所以最终内存增加,游戏速度急剧下降,最终崩溃。当我移动到 VC2 时,我需要以某种方式释放 VC1,然后在移动到 VC3 时释放 VC2,等等。每次我围绕 VC 进行循环时,我希望它们重新启动,就好像它们以前从未存在过一样。我将提供我如何在所有人之间转移的代码。顺便说一句,在 xcode5 中使用精灵套件。VC = 视图控制器

VC1内部:

   @implementation HomeScreen

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)goToGame:(id)sender {

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"GameScreen"];
[self presentViewController:vc animated:YES completion:nil];


}  //This moves me to my VC2

VC2内部:

@implementation ViewController



- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

  [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(presentMyViewController) name:@"presentMyViewController" object:nil];

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;


// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:scene];
}


-(void)presentMyViewController
{
[self performSegueWithIdentifier:@"Segue" sender:nil];
}

在连接到 VC2 的 SKScene 内部:

 @implementation
{
    int _lives;
   BOOL _gameOver;
}

- (void)update:(NSTimeInterval)currentTime
{
    if (_lives == 0 && !_gameOver) {

    _gameOver = YES;
    NSLog(@"You Lose!");
    [self presentVC3];
}

-(void)presentVC3
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"presentMyViewController"   object:nil]; 
}// This moves me to my VC3

VC3内部:

 @implementation ViewController3



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
    // Custom initialization
   }
  return self;
}

- (void)viewDidLoad {
{  
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



- (IBAction)goToHomeScreen:(id)sender {

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard    instantiateViewControllerWithIdentifier:@"HomeScreen"];        
  [self presentViewController:vc animated:YES completion:nil];


} // This move me back to VC1

VC1 是一个 UIViewController,VC2 是一个 ViewController,它呈现一个 SKScene,这是我的实际游戏,VC3 是一个 UIViewController。

所以只是重申一下。当我从 VC 移动到 VC 时,它们都相互重叠,增加了内存和 CPU,并降低了游戏的 FPS(每秒帧数),直到最终崩溃。我被告知我需要关闭我的 VC 或从内存中释放它们来解决这个问题,如果这是正确的解决方案,那么我该怎么做呢?如果您有正确的解决方案,请提供一些代码以提供帮助。任何帮助表示赞赏!

4

1 回答 1

0

我很少使用 SK,但 SKView 是一个 UIView 子类,您可以将场景放入其中。也许您正在寻找的设计使用单个视图控制器,只是切换它的 SKView(或者它是 SKView 的场景)。

您使用的 presentVC... 逻辑本质上会堆叠 vcs,因此您将很难(不可能)一次只保留一个。如果您必须更改 vcs,则可以在窗口的根视图控制器级别进行更改。

于 2014-07-17T20:10:45.157 回答