Placing background node covers all other nodes level transition - sprite-kit

When the game changes levels I have a method that loads a new background and also changes several global properties of the SKScene ie enemy speed etc.
Problem is when I redraw the background in a new level it covers all other nodes (created in initWithSize) . Is there a work around or a better approach redrawing the background?

If I understand what you are saying you have a level which has a background node and other nodes that are displayed during the level. When you change levels, you add a new background, presumably as a child of SKScene, correct?
If this is the case, that is why it covers everything. By adding it later onto the node tree, it gets drawn last, and hence covers everything.
There are a few ways you can handle this:
-Have a different scene per level. This way each scene is self contained and will not interfere with the other scene's contents.
-removeAllChildren on the SKScene, and then add your background and anything else you need for the new level.
-If you really wanted to, you could just replace the texture for the original background with the newer background. But if you do this, you still potentially need to clean up old nodes.
Having a different scene is probably the better option out of the bunch.

Related

How should I handle a multiple scenes project?

I'm trying to make this game using the approach of multiple scenes to make things more modular.
In my actual case I have an "Initialization" scene which holds some global state objects and the one to control the state machine of all the scenes in the game.
As for the other scenes, for now I divided them just in two: the base scenes (which for now contains everything besides UI) and its UI scenes (which basically have a Canvas and all the UI elements and UI-related scripts).
The confusion in my mind is simple though: as I tried to make the UI scenes as modular and independent as possible, there are a lot of points of interactions between the base scene and its UI scene.
For the sake of illustrating this question please take this problem I'm facing right now: I have camera animations that should be played as a response to user inputs to the UI (like the click of a button should trigger a specific camera animation). Thing is: that camera is not in the UI scene. The way I'm resolving this problem right now is creating a ScriptableObject which holds events for important actions triggered in the UI scene that are fired in the UI scene and subscribed in any other place. The same can occur in the opposite direction: the UI scene need to react to many actions that happens in other scenes.
Considering that the "camera animation" problem I explained above can happen with many other objects, if there is not a better way to handle that wouldn't splitting a game into multiple scenes be just too much of work just for the benefit of modularity? And by that I also asks: am I handling this problem the right way?
If you want to keep things consistent between scenes, there are a few ways to do it.
PlayerPrefs lets you keep variables consistent, I don't need to do a whole tutorial here, look it up.
DontDestroyOnLoad lets you take an object and make it consistent throughout the whole game. If you want, you can use DontDestroyOnLoad on one of your cameras and just delete the others in the other scenes if you want to keep a consistent camera.

Use one or multiple SKScenes for Level based Game

I'm making a level based game using SpriteKit. I would like to know the best practice for making level changes. I was originally using one Scene as my gameplay scene and when a level is completed, it removes all nodes in the scene and then adds the ones for the next level. I am using a background node that is persistent throughout the entire course of the game. I'm worried about memory because I don't think ARC will deallocate the nodes removed because the scene is persistent. Is this method ok or should I instantiate a new scene for when a level is changing?
The best practice is to separate game data and assets from game code. This way, you can make changes to stuff without having to recompile (and is handy if you are working with another person who doesn't code).
Here is what apple has to say about it at WWDC 2014:
When you transition between scenes, ARC will deallocate the prior scene, assuming you made no strong references to it. Since you're starting out it's unlikely you need to worry about this right now... it's mostly done with globals and closures, and you should be able to fix it if it becomes a problem (but likely won't).
You can use things like unowned self and weak var etc to ensure this doesn't happen when needed. Again this is a more advanced topic but it's good to be aware of them.
Really, making level changes is entirely up to you... if you want to make a state manager that swaps out stuff to one scene, you can certainly do that... or, you could make a bunch of scenes and transition to that. Again, the best practice here is separating game content from game code, not necessarily how you switch scenes.
Personally, I would used separate SKScenes-- because it's already built in with transitions, memory management--and you have the option of using the editor, and you get to give each scene it's own file if desired.
There is also GameplayKit which has a statemanager, in which case you could use one scene and have different states be the level.
here are some resources, buried in there are some nuggets pertaining to what you want.
https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/DesigningGameswithSpriteKit/DesigningGameswithSpriteKit.html
https://developer.apple.com/videos/play/wwdc2014/608/
https://developer.apple.com/library/content/samplecode/DemoBots/Listings/DemoBots_SceneManager_swift.html

Should I remove all SKSpriteNodes and Labels when I switch from one node to another

My iOS game has a couple of scenes. I've noticed some lag between switching scenes, and I was wondering if it might be because I'm not removing all nodes and labels from parents when I transition to another scene. Is it good practice to remove all nodes from their parent when transitioning to another scene?
Also, I've noticed that when I do remove all nodes, the transition effect is kind of ruined, as the screen goes all black during the transition.
Is it possible to delete nodes(of previous scene) after the transition to next scene?
When you perform the transition, the scene and its nodes will be released from memory, unless you have a strong reference cycle. Also, you should know that SpriteKit has its own cache system for the SKTextures, so not all memory will freed.
The lag could be caused by a lot of thing, some possibilities:
If you instantiate the new scene on touchesEnded (or your custom button callback closure), the lag could be caused because you're doing too much work on the initialization. This can be solved by, for example, preloading the scene with a closure that run in background and, when you have to run the transition, you already have everything loaded. An example follows:
Maybe you're using assets that are too large and because they take longer to be loaded, you have the lag. You could solve this by, for example, converting images that don't need an alpha channel to .jpeg.
Another solution would be to preload assets. A code example follows.

How to show 2 scenes on screen at the same time

How can I show 2 scenes on the screen at the same time on Sprite Kit Swift?
I need to first scene be placed of the second (I need one scene to be seen and atop of it appeared other scene, which has some sprites).
Or tell me, how developers make, for example, menu (with buttons like "restart", "results") to appear on other scene when player dies? And that time, also you can see the main scene with location (or with dead character)?
You do not need 2 scenes. However, you can accomplish what you are after with basing an node tree off of an SKNode. For your menu idea you could build it out of however many objects you'd like, but since it is based from an SKNode, you can animate it to your hearts content. No need for two scene, just build another node tree and animate the root. If that isn't enough answer, I could write some code to help you out if need be. Sorry watching the Netflix and feeling lazy.

Spritekit: Change a scene and keep the background

I'm developing a game that needs an independent animated background. The game need to change some scenes, keeping the same background. Depending on user input, the background will animate accordingly.
Since the scene's background moves with the scene, on transition, how can I achieve this? The only way I can imagine is have 2 SKViews, each with a scene. So I can have a background scene and a foreground scene separately. But I have no idea if it is possible and how can I do this.
Any help is appreciated!
My best,
Gui
You can't preserve nodes across scenes. You must add the node again during each scene transition. This is why I do not use Apple's scene transitions the way the documentation describes. Instead I use my own custom SKNodes and make my own custom transitions using those nodes, which I highly recommend you do. I gave a detailed answer about this here