正确的移动到另一个.SKS文件的方式

时间:2017-02-19 21:40:59

标签: swift xcode

我有一个游戏,我有两个场景:FarmScene和WoodScene。 每个场景都有一个.SKS文件和一个.swift文件 - 一个用于设计,另一个用于编码。 我设法从FarmScene转移到WoodScene,如下所示:

    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移动到不同的场景,并且我可以做一些很酷的过渡,如翻转,淡入淡出和推送。

我想知道这是不是&#34;正确&#34;在Xcode中使用场景设计器时改变场景的方法?或者也许我错过了一些东西。

期待收到你的来信。

1 个答案:

答案 0 :(得分:1)

我不会像循环这样做,只是为了避免潜在地运行该演示代码不止一次。也许使用保护声明设置您的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]))

}

我不会说有正确或错误的方式,只是偏好你希望过渡看起来多么花哨。