Spritekit: Change a scene and keep the background - swift

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

Related

Does setting camera's culling mask to Nothing when UI covers the screen is good?

I have a pretty large and full scene and therefore gets a lot of draw calls.
Sometimes I display a video in the game, which covers the entire screen.
When I tested my game with Unity's profiler tool I noticed that the camera still renders everything (although occlusion culling is enabled and calculated), and it causes the video to lag.
My question is how can I disable the camera?
When I disable the Camera component or the camera's GameObject I get a warning ⚠ in the game view that says No camera is rendering to this display. Which, I guess, is not good (correct me if I'm wrong).
So I was wondering if cancelling the culling mask on the camera (by setting it to Nothing) would force unity to stop render the scene.
Or does it still do some work in the background?
(Like with UI elements that still being rendered even though they are fully transparent).
Thanks in advance
I have a pretty large and full scene and therefore gets a lot of draw
calls.
I recommend activating "Instancing" on your materials, it can greatly reduce draw calls.
When the UI Pops open, it can help removing the "Default" layer (or whatever layer the majority of your renderers are) from the active cameras. You can do this easily with layer masks. Or you can just set Camera.main.farClippingPlane to 1 or any low number.

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.

Capturing full background with camera view in 2d

So I have a small problem with my camera in 2D view. I have a background sprite that serves just as it sounds, the background. My problem is, I need the camera view to take up the entire background. At the moment I can only get the camera to capture out to the sides of the background but it will not fully catch the top and bottoms of the background.
This poses a problem, it will cause me to have to make all of my following items in the project to be super short and fat.
Yes, I know this is probably a stupid issue on my part or an easy fix, but being new to Unity, I am unaware of how to deal with this problem.
Here is a snip of my issue http://imgur.com/pz1sg9B
As you can see, I cannot get the camera to reach the top and bottom of the background thus leaving me with super short and fat items.
Thank you in advance!
You have to change the Viewport Rect on the editor.
You can check out Unity3D Manual for more knowledge on Cameras in Unity3D.

Placing background node covers all other nodes level transition

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.