How to deallocate unused SKScene - ios

I have a SKScene object setup in GameViewController. I implemented dealloc into the SKScene subclass as follow:
- (void)dealloc {
NSLog(#"Dealloc: %#", self);
}
Now I want to present another view controller on top of GameViewController. I used storyboard segue to do this. But after the new view controller is presented, I never received the SKScene dealloc. It got stuck in the memory. My app starts to freeze after a few minute due to low memory. How can I dealloc the scene after presenting the new view controller.

In order to deallocate you SKScene instance -> you Should Eliminate every pointer that retains it -> then ARC will automagically release it.
Simply call the [skView presentScene:nil]; method which also will remove the SCScene and deallocate it by setting SKView.scene property to nil.
SKView *skView = (SKView *)self.view;
GameScene *scene = (GameScene *)skView.scene;
for(SKNode * child in scene.children)
{
[child removeAllActions];
}
[scene removeAllChildren];
[scene removeAllActions];
[skView presentScene:nil];
Note Normally you don't have to remove everything from the SKScene if your memory is managed right just call [skView presentScene:nil]; method and ARC will take care of it.
Apparently you had something inside the SKScene that retained it.
So by removing everything from it, we eliminated the retain cycle.
This solution will not always work, it will work only if one of the SKActions or SKNodes are retaining the SKScene which were the issue in your case, However your SKScene could be retained from somewhere else and this answer wouldn't really help you

Related

SpriteKit memory issues

I'm new to game development with SpriteKit and everything went ok so far, but cant really the memory management part.
I created a first UIViewController to act as a level selector, a second UIViewController in which a present the SKScene of the game. The problem appears when i go back from the SKScene to the level selector and none of the memory is released.
From the game SKScene, when the user pushes the back button to go to the level selector i post a notification which tells the second UIViewController to perform the segue.
//SKScene
[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];
[[NSNotificationCenter defaultCenter]postNotificationName:#"toLevelSelector" object:nil userInfo:nil];
//Second UIViewController
- (void)toLevel:(NSNotification *)notif
{
[self performSegueWithIdentifier:#"toLevelSelector" sender:self];
}
Can you help me get a better understanding on when and where should be the skview or the skscene released from memory ?
I think the memory is not released because you did not dismiss view controller that holds SpriteKit. When I first tried to make game that has menus in UIViewControllers and then button starts SpriteKit game, I experienced that closing game does not end timers, sounds, music, etc...
Let's say you have UIViewController called MainMenuViewController and that it has a button that calls GameViewController in which you run SpriteKit Game. So in order to close your game completely, try to implement this method in your exit game button
[(GameViewcontroller *)self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

Cannot present scene with SpriteKit

I have two SKScene SceneA,SceneB.
I have implemented two methods(willMoveFromView,didMoveToView) in each scene.
Assume, I am currently in SceneA and i want to present SceneB via this code block:
SceneB *sceneB = [SceneB sceneWithSize:self.view.bounds.size];
sceneB.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:sceneB];
When I present this scene, I can see this:
sceneA willMoveFromView
sceneB didMoveToView
sceneA init
sceneB willMoveFromView
sceneA didMoveToView
And finally I return to first SceneA ! There is no other code that pushes SceneA from SceneB . I don't know what to do, please help
UPDATE:
As #LearnCocos2D says, viewWillLayoutSubviews is forced every time I present scene
And I present sceneA in viewWillLayoutSubviews with following code
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView* skView = (SKView*)self.view;
SceneA *sceneA = [SceneA sceneWithSize:self.view.bounds.size];
sceneA.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:sceneA];
}
As #LearnCocos2D says, viewWillLayoutSubviews is forced repeatedly , And I used to create new scene everytime it has been called. So I need to check if SKView.scene property is not nil. Here is the code:
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView* skView = (SKView*)self.view;
if(!skView.scene){
SceneA *sceneA = [SceneA sceneWithSize:self.view.bounds.size];
sceneA.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:sceneA];
}
}

How to dismiss SKScene?

When Im finished with my SKScene is there a way to dismiss the SKScene from within my SKScene class?
If not back in my Viewcontroller where I present my SKScene [skView presentScene:theScene]; is there a way to restart the scene or remove in from my SKView?
The SKScene Class Reference and SKView Class Reference are no help.
Update:
The following code removes my scene from my SKView [yourSKView presentScene:nil]; but when Im back in my view controller the scene is still running in the background. I can always pause it when the game is over and I'm sent back to my view controller(menu) but I'm wondering is there another method other then pausing it like completely removing it?
-(void)endTheGame {
[highscoreLabel removeFromSuperview];
NSLog(#"Game Over");
//would like to end here before calling the below method in my view controller
[self.delegate mySceneDidFinish:self];
}
Having met a similar issue I stumbled around your question, and since nobody gave a decent answer here's how I solved it:
in my scene I called both lines
[self removeFromParent];
[self.view presentScene:nil];
in my controller (the controller that displays the SKScene) I changed the template code from Apple, which was creating and presenting my scene from viewDidLoad in order to create and present my scene in viewWillAppear, only if the view's scene is nil
here's my Swift code, even if you're using Objective C you'll understand what it does; the key line being the "if skView.scene == nil" test :
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let skView = self.view as SKView
if skView.scene == nil {
let scene = GameScene(size:skView.bounds.size)
scene.controller = self
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
You can use:
[yourSKView presentScene:nil];
to remove the scene.
"You can't go "Back to the View Controller" from a scene. The scene is a View, the view controller controls and displays views. Use the view controller to change views. Remember the view controller itself is not a view." -Wharbio
Best solution here is to create another View Controller. This view controller will be my menu. Then the other viewcontroller will act as a host for the skscene.
In my situation I then use my menu viewcontroller to dismiss the viewcontroller displaying in the skview.
From within your SKScene, you can simply do [self.view presentScene:aNewScene] to present another scene
I completely remove scenes by using this block in my view controller:
Obviously you will need to declare your Size, and "newSKview"
SKView * skView = (SKView *)self.view;
SKScene *newScene = [[newSKView alloc]initWithSize:size];
newScene.scaleMode = SKSceneScaleModeAspectFill;
SKScene *oldScene=(skView.scene);
[oldScene removeFromParent];
[skView presentScene:newScene];
This works fine for me. None of my Scenes are retained or strong.
I prefer to use an additional UIViewController for SKView and Notification Center in where the UIViewController made SkScene.
The callback function is
#objc func cb_dismissingVC(_ notificaiton: Notification) {
self.dismiss(animated: true, completion: nil)
}
Maybe my variant will be helpful:
[[self.scene childNodeWithName:#"YourChildNodeName"] removeFromParent];

SpriteKit view throwing NSInvalidArgumentException

I have this code below that loads a scene in the viewDidLoad: method.
SKView *spriteView = (SKView *) self.view;
spriteView.showsDrawCount = YES;
spriteView.showsNodeCount = YES;
spriteView.showsFPS = YES;
PFPiePlanesScene *scene = [PFPiePlanesScene sceneWithSize:self.view.frame.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[spriteView presentScene:scene];
When I call anything on the spriteView, it crashes with this message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsDrawCount:]: unrecognized selector sent to instance 0x9685030'
I assume this is because it is not treating the instance as a SKView and instead as a UIView.
Thanks in advance.
Some extra:
Sprite kit uses this code to load a scene.
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
// Create and configure the scene.
self.scene = [SKMyScene sceneWithSize:skView.bounds.size];
self.scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:self.scene];
And, with no error. The classes are the exact same. Would just coming into the view screw this up?
Joe
If you are following apple's walk-thougrh you skipped the part : "Open the storyboard for the project. It has a single view controller (SpriteViewController). Select the view controller’s view object and change its class to SKView.
self.view is a UIView object. Simply casting the pointer does not make it a different type of object.
SKView *spriteView = (SKView *) self.view;
spriteView is now pointing to the object, that self.view is pointing to, telling the compiler, that it actually points to an object of type SKView. That's why you don't get any compiler errors or warnings. At runtime you are sending messages that can't be handled by spriteView because it is still just a UIView object.
Don't forget add SpriteKit.framework under Linked Frameworks and Libraries.
Cheers,
Jesse
what you have to do is to change the subclass of view from UIView To SKView select ur viewcontroller under that view and change it subclass
#Sebastian is not correct.
i guess you are following apple's walk-thougrh.
maybe the following tutorial will help:
http://www.raywenderlich.com/49625/sprite-kit-tutorial-space-shooter
Before casting your UIView to SKView , you must make sure to set your custom class as SKView in your storyboard.
If you notice the first view controller comes with these imports in the header file.
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
When you create a new view controller, by default Spritekit is not included. After adding sprite kit it should work with the default code apple provides

Stop Animation with CCDirector AFTER adding UIViewController to cocos2d scene

I'm trying to add a UIKit ViewController on top of a cocos2d scene. Everything works great, but I need to run [[CCDirector sharedDirector] stopAnimation] AFTER the UIKit view is loaded completely.
I push a button and have a 1 second transition to the new scene being loaded. I have tried adding stopAnimation to every callback in both the UIViewController(ViewDidLoad, ViewDidLayoutSubviews, etc) and the Cocos2d scene. What happens is the scene is loaded but the animation is stopped before the UIViewController is completely loaded, which prevents the view from loading at all.
I could set a timer for the transition duration and then call stopAnimation but I would rather not do it that way. Is there any sort of delegate method I'm missing here?
You need to call that method from your view controller's viewDidLoad method:
- (void) viewDidLoad
{
[super viewDidLoad];
[[CCDirector sharedDirector]stopAnimation];
}

Resources