2017-02-19 21 views
0

我有一個遊戲,我有兩個場景:FarmScene和WoodScene。 每個場景都有一個.SKS文件和一個.swift文件 - 一個用於設計,另一個用於編碼。 我已經成功地從FarmScene移動到WoodScene這樣的:移動到另一個.SKS文件的正確方式

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let location = touch.location(in: self) 
     let node : SKNode = self.atPoint(location) 
     if node.name == "WoodIcon" { 
      if let view = self.view as! SKView? { 
       // Load the SKScene from 'GameScene.sks' 
       if let scene = SKScene(fileNamed: "WoodScene") { 
        // Set the scale mode to scale to fit the window 
        scene.scaleMode = .aspectFill 

        // Present the scene 
        view.presentScene(scene) 
       } 
      } 
     } 
    } 
} 

在我以前的比賽,我已經使用了SKTransition移動到不同的場景,並與我所能做的像翻轉,淡入淡出一些很酷的轉變並推動。

我在想,如果這是在Xcode中使用場景設計器時更改場景的「正確」方式嗎?或者也許我錯過了一些東西。

期待您的來信。

回答

0

我不會這樣做的一個循環,只是爲了避免潛在地運行該演示文稿代碼不止一次。也許設置您touchesBegan法保護聲明

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    // Guard to just use the first touch 
    guard let touch = touches.first else { return } 
    let location = touch.location(in: self) 
    let node : SKNode = self.atPoint(location) 
    if node.name == "WoodIcon" { 
     if let view = self.view as! SKView? { 
      // Load the SKScene from 'GameScene.sks' 
      if let scene = SKScene(fileNamed: "WoodScene") { 
       // Set the scale mode to scale to fit the window 
       scene.scaleMode = .aspectFill 

       // Present the scene 
       view.presentScene(scene) 
      } 
     } 
    } 
} 

此外,如果你想這樣做,通過代碼,你可以做這樣的事情一個自定義的轉換:

func customTransition() { 

    // Wait three seconds then present a custom scene transition 
    let wait = SKAction.wait(forDuration: 3.0) 
    let block = SKAction.run { 

     // Obviously use whatever scene you want, this is just a regular GameScene file with a size initializer 
     let newScene = SKScene(fileNamed: "fileName")! 
     newScene.scaleMode = .aspectFill 
     let transitionAction = SKTransition.flipHorizontal(withDuration: 0.5) 
     self.view?.presentScene(newScene, transition: transitionAction) 

    } 
    self.run(SKAction.sequence([wait, block])) 

} 

我不會說有一個正確或錯誤的方式,只是喜歡你想要過渡看起來多麼華麗。

相關問題