Search code examples
iossprite-kitskscene

Is it possible to push a Scene on top of another in Sprite Kit?


When transitioning between SKScenes, is it possible to keep the first scene around and push the second scene on top of the first scene, so that it is possible to return to the first scene? I.e. does something like pushviewcontroller exist for skscenes?


Solution

  • Yes, it's possible. You just need to have a strong reference to the currently disabled scene, for instance in a property of the view controller, to prevent it from deallocating and to be able to present it again when you "pop" the "pushed" scene.

    See Kobold Kit's implementation of pushScene/popScene in its SKView subclass, which uses a regular array to hold on to the scenes on the "push stack": https://github.com/KoboldKit/KoboldKit/blob/master/KoboldKit/KoboldKitFree/Framework/View/KKView.m