0

i have 3 view controllers. VC1(initial & home screen), VC2(SKScene & game), VC3(UIViewController & Gameover screen). My problem is when i start with VC1, then go to VC2 and then move to VC3 and then back to VC1, they all stack up on top of each other so it looks like this. VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3, so eventually the memory builds up and the game slows down drastically eventually crashing. I need to somehow release VC1 when i move to VC2, then release VC2 when moving to VC3, etc. Everytime i make a loop around the VCs i want them to restart as if they were never there before. I will provide code of how i transfer between all of them. btw using sprite kit in xcode5. VC = View Controller

Inside 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

Inside 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];
}

Inside SKScene which is connected to VC2:

 @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

Inside 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 is a UIViewController, VC2 is a ViewController that presents an SKScene which is my actual game, and VC3 is a UIViewController.

So just to restate. As i move from VC to VC they all overlap onto one another increasing the memory and CPU and decreasing the FPS(frames per second) of the game until eventually crashing. I was told i need to dismiss my VC or release them from memory to fix this problem, if that is the correct solution then how do i go about doing that? If you have the correct solution please provide some code to help. Any help is appreciated!

ncd275
  • 1
  • 3
  • Why don't you use segues? – Andrey Gordeev Jul 16 '14 at 03:38
  • because I am not familiar with them, can you show me some code or an example. and do segues dismiss VCs from memory? – ncd275 Jul 16 '14 at 06:02
  • There are tons of examples all over the web, how to use segues. Yes, you don't have to worry about deallocating VCs with segues – Andrey Gordeev Jul 16 '14 at 06:58
  • I am sure there are examples all over the web but I am looking for an example specific to my problem so that's why I am asking this question. So unless you can provide some code/method to help me out then don't bother. I came to this website asking this question because I could not find an answer on the web. I am new to programming and I am trying to learn off of books that I bought so being self-taught is kind of a challenge that is why I need some actual code to get me started. It is hard for me to apply from other people's problems that's why I am looking for a solution specific to mine. – ncd275 Jul 16 '14 at 18:14
  • Check this answer, it could help you: http://stackoverflow.com/questions/21578391/presenting-uiviewcontroller-from-skscene/21674841#21674841 – Andrey Gordeev Jul 17 '14 at 08:53
  • i have figured out how to dismiss my VC1 and VC3 but not my VC2 which presents my SKScene. Any idea how to dismiss an SKScene in sprite kit? – ncd275 Jul 17 '14 at 19:46
  • What do you mean? You don't need to "dismiss" anything. Just switch between view controllers, that's it – Andrey Gordeev Jul 18 '14 at 01:37
  • If I just use segues then the memory and CPU get to high and the app crashes. because as I switch through the VCs they all stack up on one another and eat up the memory – ncd275 Jul 18 '14 at 06:23
  • You don't need to use multiple view controllers to implement your game. You can just present the various scenes as needed. – 0x141E Jul 25 '14 at 05:10

1 Answers1

0

I don't use SK much, but the SKView is a UIView subclass that you put an scene into. Maybe the design you're looking for uses a single view controller just switching it's SKView (or it's SKView's scene) around.

The presentVC... logic you're using inherently stacks vcs, so you'll have a tough time (impossible) keeping only one at a time. If you must change vcs, the way to do that is at the window's root view controller level.

danh
  • 62,181
  • 10
  • 95
  • 136
  • Okay thank you. And just an off-topic question. Do you by chance know how to pass data from one Viewcontroller to another. In My second view controller(SKScene) which is my actual game i have a private int called score. well when they game is over i want to be able to display that int score onto the 3rd VC. How can i achieve this? – ncd275 Jul 17 '14 at 20:21