首页 文章

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

提问于
浏览
0

我有3个视图控制器 . VC1(初始和主屏幕),VC2(SKScene和游戏),VC3(UIViewController和Gameover屏幕) . 我的问题是当我从VC1开始,然后转到VC2,然后转到VC3然后再回到VC1,它们都叠加在一起,所以它看起来像这样 . VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3,最终内存逐渐增加,游戏速度大幅下降,最终崩溃 . 我需要以某种方式释放VC1,当我移动到VC2,然后在移动到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

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

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

相关问题