2016-07-27 118 views
1

我有兩個場景:家庭和遊戲。與過渡到主場景相比,過渡到場景真的很慢。我認爲這是因爲我的比賽場面更多。有什麼方法可以預先加載場景嗎?或者讓過渡更加無縫?緩慢Skscene轉換

我對這個論壇的答案感興趣Preload a Scene to prevent lag? 但我不知道從哪裏開始。我在哪裏放置部分和B部分的答案

這裏是我使用

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)])) 


    }  

什麼任何其他解決辦法是罰款。

感謝

回答

1

我做這做工作: 在didView HomeScenepreloadGameScene() 而且總是在家裏的一幕:

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()通過按鈕個人SKButton類叫做:

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)) 
      }) 

     }) 
    } 
+0

嘿感謝的響應。我爲swift 3試了這個,它不起作用。你能否更新你的答案? – Yraw

+0

@Yraw檢查更新 –

+0

它給我一個錯誤「Gameconfiguration沒有成員級別」。 – Yraw