Reputation: 31
I am creating a game which possess upto 5 different scenes(levels). However there are multiple scenes, I would want to have only one endless background scrolling forever, even when i transition from one scene to another.
Couldn't find anything on google so expect the answer here.
Upvotes: 2
Views: 183
Reputation: 10674
Its always good practice to post some code of what you have tried before when asking a question on stack overflow. People tend to not help when you show no code.
Since you cannot have multiple SKScenes running at the same time and scenes start in a clean state (unless you archive the whole scene) you will most like have to use your current set up for the scrolling background and use it for all scenes. You can use protocol extensions or subclassing to share the code.
As KingOfDragon has so kindly pointed out, you can try playing around with SKTransitions, such as pushInDirection to get the desired result.
If that doesn't work and you want the scrolling background to resume at the exact spot in a new scene you could create some property that stores the current background position before a scene change. Than after the scene change you set the background to the new position so it looks the same.
Im not sure what your setup is but of the top if my head it could be something like this
Create a global property above any scene class
let bgPosition = CGPoint()
Than create a protocol extension for your scrolling background code to make it reusable
protocol ScrollingBackground { }
extension ScrollingBackground where Self: SKScene {
func loadScrollingBackground(at position: CGPoint) {
/// your code for the scrolling background
/// set position to position you pass in function
}
Than in your first scene you conform to the protocol and set your scrolling background as usual. Than add the WillMoveFromView method that gets called upon scene transition. There set the bgPosition property to your last position of the scrolling background.
class Scene1: SKScene, ScrollingBackground {
....
let backgroundPosition = ....
loadScrollingBackground(at: backgroundPosition)
override func willMoveFromView(view: SKView) {
bgPosition = ... //Set to current position of scrolling background
}
}
Than in your other scenes you do the same but pass in the bgPosition property (as mentioned above) to set the background to the position it was in Scene1.
class Scene2: SKScene, ScrollingBackground {
....
loadScrollingBackground(at: bgPosition)
}
Not sure this is exactly what you are looking but something like this is probably the easiest way.
Hope this helps
Upvotes: 2