I have two scenes: Home and Play. The transition to play scene is really slow compared to the transition to home scene. I think this is because there's more happing in my play scene. Is there any way I can preload play scene ? Or make the transition more seamless?
I'm interested in the answer in this forum Preload a Scene to prevent lag?
but I have no idea where to begin. Where do I place part A and B of the answer
Here is what I'm using
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches
let location = touch.first!.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "Balloon") {
let sceneAction = SKAction.runBlock({
let secondScene = Scene2(size: self.size)
let transition = SKTransition.fadeWithDuration(1)
secondScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view?.presentScene(secondScene, transition: transition)
})
PlayButton.runAction(SKAction.sequence([sound,SKAction.animateWithTextures(array, timePerFrame: 0.1),sceneAction,SKAction.waitForDuration(1)]))
}
Any other solution is fine.
Thanks
I do this and works:
in didView of HomeScene put preloadGameScene()
And always in home scene:
fileprivate var nextScene: SKScene?
func preloadGameScene () {
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
self.nextScene = GameScene(fileNamed:"yoursksfilename")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("SCENE LOADED")
let loading = self.childNodeWithName("Loading") as? SKLabelNode
self.playButton?.hidden = false
self.playButton?.alpha = 0
//loading?.hidden = true
self.playButton?.runAction(SKAction.fadeAlphaTo(1.0, duration: 0.4))
loading!.runAction(SKAction.fadeAlphaTo(0.0, duration: 0.4))
})
})
}
goToGameScene()
is called by a button personal SKButton class:
func goToGameScene () {
guard let nextScene = nextScene else {return}
let transition = SKTransition.crossFadeWithDuration(0.5)
nextScene.scaleMode = .AspectFill
scene!.view?.presentScene(nextScene, transition: transition)
}
UPDATE SWIFT 3
fileprivate var nextScene: SKScene?
func preloadGameScene () {
let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgroundQueue.async(execute: {
self.nextScene = GameScene(fileNamed:"yoursksfilename")
DispatchQueue.main.async(execute: { () -> Void in
print("SCENE LOADED")
let loading = self.childNode(withName: "Loading") as? SKLabelNode
self.playButton?.isHidden = false
self.playButton?.alpha = 0
//loading?.hidden = true
self.playButton?.run(SKAction.fadeAlpha(to: 1.0, duration: 0.4))
loading?.run(SKAction.fadeAlpha(to: 0.0, duration: 0.4))
})
})
}