如何解雇/删除SKScene和View Controllers?

时间:2014-07-15 19:54:47

标签: uiviewcontroller xcode5 sprite-kit viewcontroller skscene

我有3个视图控制器。 VC1(初始和主屏幕),VC2(SKScene&游戏),VC3(UIViewController& Gameover屏幕)。我的问题是当我从VC1开始,然后转到VC2,然后转到VC3,然后回到VC1,它们都叠加在一起,所以它看起来像这样。 VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3,最终内存逐渐增加,游戏速度大幅下降,最终崩溃。当我移动到VC2时,我需要以某种方式释放VC1,然后在移动到VC3时释放VC2等。每次我在VC周围做一个循环,我希望它们重新启动,好像它们以前从未出现过一样。我将提供如何在所有这些之间进行转移的代码。顺便说一下在xcode5中使用sprite kit。 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或从内存中释放它们来解决这个问题,如果这是正确的解决方案那么我该如何去做呢?如果您有正确的解决方案,请提供一些代码以提供帮助。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

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

您正在使用的当前VC ...逻辑堆栈vcs,因此您将有一个艰难的时间(不可能)一次只保留一个。如果必须更改vcs,那么执行此操作的方法是在窗口的根视图控制器级别。