I have a SpriteKit app. I would like to simply switch from one view class to another. In this case, I would like to just switch from one screen to another screen in the app, so that you can have different things on display that were triggered from a button press or some other logic in the code.
I am having trouble creating the second GameScene, as I get some errors. Could someone provide an example of just switching from one scene to another. Here is some code I attempted to get working:
class GameScene: SKScene {
var player = SKSpriteNode()
var ground2 = SKSpriteNode()
// The main scene that loads just fine and runs. However I want to transition to another scene, the desert scene. With logic from here to there.
}
class DesertScene : SKScene {
// inserting my variables, my logic, CGPoints, you get it.
}
func goToScene(newScene: SceneType) {
var sceneToLoad:SKScene?
switch newScene {
case SceneType.GameScene:
sceneToLoad = GameScene(fileNamed: "GameScene.swift")
case SceneType.TheWoods:
sceneToLoad = TheWoods(fileNamed: "TheWoods")
case SceneType.DesertScene:
sceneToLoad = DesertScene(fileNamed:"DesertScene")
}
}
I was not really sure how to transition between either SKS files or even if I should create a new swift file, and have it access GameScene.swift
If you try to start a new SKScene using the same view controller, the entire previous SKS will be leaked. I've encountered this issue before.
In your case, you want to start a new GameScene. I'd suggest you creating a new swift file and initialize that gamescene, as you've suggested in your question.
Check out my post here, which explains why a new SKScene shouldn't be initialized within the same VC.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.