Search code examples
iossprite-kitskaction

Sprite Kit how to delete game scene


I used profiler and detect that after I go back from view controller that contains game scene view (for example pop back to the main menu). The game scene is still in the memory.

I supposed it's connected to some SKAction I use. How track which object cause that issue.

I use some SKAction run block, repeat forever and etc, and I am sure that the something happen with it.


Solution

  • You cannot delete a SKScene. I would recommend creating a function for when your present the scene. Here is and example

    (Swift)

     func deleteView(deleteEveryThing:Bool) {
    
        if deleteEveryThing {
    
             self.removeAllActions()
             self.removeAllChildren()
    
             //Scene presentation code here                
        }
    
        else {
    
             self.removeAllChildren()
             //Scene Presentation Code here
        }
     }
    

    (Objective - C)

     -(void)deletView:(BOOL)deleteEveryThing {
    
         if (deleteEveryThing) {
    
            [self removeAllNodes];
            [self removeAllActions];
        }
    
        else {
    
            [self removeAllNodes];
            [self removeAllActions];
        }
     }
    

    So what I did was create a function called deleteView and deleteView has a parameter that is a Boolean (True or false) and if it is trure then it will remove all actions : self.removeAllActions() or [self removeAllActions]; and all children: self.removeAllChildren() or [self removeAllChildren];in the SKScene. This can help in freeing up memory and once those two lines of code ran then you can handle the scene presentation code. There is also an else part which does the same thing but leaves the SKActions in the SKScene.