Reopening scene not working

Scenario in my game:


main menu -> game setup menu -> core game scene -> game menu (load/save/quit) -> core game scene -> game menu (here stuff stops working).


Each segment is it's own SKScene and presented from one to other. Game data is stored in static class that can be accessed from all classes.

When I first time start game and come into core game scene, everything is ok. I can open game menu and close it again (returning to core game scene). But when I wish to open game menu scene again it does not fire didMoveToView function and nothing happens.

I have made all transitions between scenes with the same principle and going from main menu scene to game setup and back works normaly, I can do it multiple times. Even tried without transition, but something isn't working well here... Seems I can't find solution here 😟

Answered by C_Wiz in 125954022

Considering your needs, check the PauseScene class in this link : http://stackoverflow.com/questions/20507790/spritekit-return-to-a-persistent-scene-in-the-same-state-it-was-left-in


It keeps a reference to the previous scene so you can actually go back, which is exactly what you wanted originally I think.

Ok I have managed to get this working, but question remains on how to for example:


- pause one scene, show some menu scene and if menu is closed close menu scene and continue playing on prevous scene? As for now first scene always resets to "starting" position.

- is it possible to save positions of the sprites and manually starts all actions from some saved state?

Scene transitions are one way only, new one replaces the previous one, you can't "go back".


You can litterally pause the SKView, but that's probably not what you want : https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/index.html#//apple_ref/occ/instp/SKView/paused


There are many ways to do what you want to do. If you want to have a separate scene just for menus, you need to make sure you can save your game state and resume it by creating a new "core game scene". Ask yourself how you handle saved games, e.g. when a user quit your app and your app gets ejected from memory, that's the same problem, you'll need to be able to resume. Best practice is to have your game state separate from the way it's displayed, so don't go the "save my sprite position" way. Serialize the game state, so you can restore everything from scratch.


You could also simply pop a large semi transparent sprite on top of your scene (to gray it out, let's say) to grab all inputs (see userInteractionEnabled), and put your menu there in your core game scene. If you have animations, you may need to handle that by stopping them in the update: of your scene.


Up to you to see what makes the most sense to your game.

I have "game data" in separate static class, that is almost always available, but problem are sprites or how to put them back into same position in the middle of some action that is repeating forever. For example, imagine you have an object rotating around other object and your object always starts at the same point when you load scene from scratch. When I open menu, I wish to "pause" game and "save" all sprite states as well and wait for user to either save game to file or just return to game, at which point scene should resume and sprites not starting from same spot as they were before, but from state they were left.


Would be nice if there is a possible way to start some action at certain point. I will try one more thing: to manually save sprites positions and when "reloading" scene, to set sprites position first and then run actions over them.

If your rotation is constant, you can probably used something like elapsed time since scene start to calculate where they should be. It's probably more reliable to just go read the .zRotation property of your nodes though, and save that.

I'm not sure what you mean by starting an action at a certain point. SKAction change the properties of your SKNodes, but you can do that manually too. So let's say you have an action that rotate something endlessly, before starting the action on your node, you simply set .zRotation back on the node, then run your action. Does that make sense ?


Edit : Check that answer which is probably closer to what you want : http://stackoverflow.com/questions/20507790/spritekit-return-to-a-persistent-scene-in-the-same-state-it-was-left-in


You can also directly serialize the whole scene yourself : https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Articles/creating.html#//apple_ref/doc/uid/20000950-SW2


Taken from http://stackoverflow.com/questions/20507790/spritekit-return-to-a-persistent-scene-in-the-same-state-it-was-left-in


(I actually didn't know you could archive a whole scene !)

This make total sense and I was already looking at some code before. But my current problem remains and it is not directly tied to saving data or serialization but with starting in mid-action.


I have objects rotating around some other object on ellipses as paths. Objects always starts from the same point on the right side of screen, no matter if I set sprite location to somewhere else on the path.

What I would like to do is:

-save sprites positions and rotations to static class I already use, before exiting scene

-show other scene if I need to

-get back to original scene and load sprite positions and rotations from static class and run actions from there (not from begining).


This static class that holds all info about state of objects and sprites will then be serialized as save game.


For rotation o sprite is is a no-brainer. Just set zRotate and then apply skaction.

Action to follow path from certain point on path is way more tricky... and I can't fiand a way to make this work... 😟

Accepted Answer

Considering your needs, check the PauseScene class in this link : http://stackoverflow.com/questions/20507790/spritekit-return-to-a-persistent-scene-in-the-same-state-it-was-left-in


It keeps a reference to the previous scene so you can actually go back, which is exactly what you wanted originally I think.

Ok if I get this obj-C "mumbo jumbo" correct, then I create a protocol with weak reference property and function to return a scene to which we should return to?

Implementing this protocol in my "main game scene" I store current scene into a weak reference property that is paused and passed to new scene and then reused there to return to "paused" scene?

Meh I'm lost in this syntax... can't get this work in swift.

I made protocol:


protocol PauseScene
{
    weak var returnScene: SKScene? {get set}
   
    func setReturnScene(otherScene: SKScene)
}


and I implemented it in my scene, but I get error: Method "setReturnScene" with Objective-C selector "setReturnScene:" conflicts with setter for "returnScene" with the same Objective-C selector...


😕

Not a protocol. PauseScene is a class. It extends SKScene.

returnScene is a weak reference property of type SKScene (a pointer) added to that class.

Thanks mate. got it working, just a few minor glicthes with some exceptions but this is only matter of properly initializing stuff 🙂

hi, can i ask how you got it working in Swift ? Thanks 1

Reopening scene not working
 
 
Q