【问题标题】:Switch between Scenes in Sprite-Kit (Swift) using LabelNode使用 LabelNode 在 Sprite-Kit (Swift) 中的场景之间切换
【发布时间】:2015-11-13 15:47:58
【问题描述】:

我使用SKLabelNodes 作为按钮,并在菜单助手中使用if 来检测对它们的触摸。打印有效(检测也有效),但如何正确切换到我的游戏场景?

func menuHelper(touches: NSSet) {
        for touch in touches {
            let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
            if nodeAtTouch.name == "title" {
                print("Title pressed")
            }
            else if nodeAtTouch.name == "newGame" {
            let scene = GameScene()
            }
        }

}

【问题讨论】:

    标签: swift sprite-kit skscene


    【解决方案1】:

    您只需要制作一个新场景(和过渡,但这是可选的)并使用适当的 SKView 方法呈现它:

    else if nodeAtTouch.name == "newGame" {
    
       let transition = SKTransition.fadeWithDuration(1.0)
    
       let nextScene = GameScene(size: scene!.size)
       nextScene.scaleMode = .AspectFill //set the scale mode like you did in your view controller
    
       scene?.view?.presentScene(nextScene, transition: transition)
    }
    

    如果没有转换,您只需使用 presentScene 方法而不是 presentScene:transition

    【讨论】:

    • 它有效,谢谢。但我仍然不太确定你的和我的有什么区别。基本上是你的最后一行吗?
    • @FelixF 是的,这是重要的部分。我还用旧场景的大小初始化了新场景的大小并设置了缩放模式。所以,正如我所说,重要的部分是你必须实际呈现新场景来替换当前场景。仅创建新场景不会自动执行此操作。希望这是有道理的!
    • 谢谢,现在完全有道理了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多