Swift 中的主菜单

Main Menu In Swift

我想在 swift 中为我的游戏创建一个主菜单。

我正在使用以下代码:

import SpriteKit

class 菜单场景:SKScene {

//Adding Start Button
let startButton = SKSpriteNode(imageNamed: "playButton")



override func didMove(to view: SKView) {


    //Temporary Background
    backgroundColor = SKColor.darkGray

    //Start Button
    startButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
    addChild(startButton)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self);  //Finding location of touch

        if atPoint(location) == startButton {

            if let scene = GameScene(fileNamed: "GameScene") {
                scene.scaleMode = .aspectFill
                view!.presentScene(scene, transition: SKTransition.doorsOpenVertical(withDuration: 1))
            }

        }
    }
}

}

当我 运行 但是,如果 atPoint(location) == startButton {. "Thread 1, Breakpoint 1.1"

我不太确定这是什么,但我希望有人能提供帮助。谢谢!

自定义SKViews

比方说,像这样M.W.E.,你想要一个菜单​​,一个难度,一个游戏场景。

然后你可以做一系列的自定义SKViews来过渡。

游戏视图控制器

此代码加载 menuScene:

override func viewDidLoad() {
    super.viewDidLoad()

    let menuScene = MenuScene(size: view.bounds.size)

    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = true
    menuScene.scaleMode = .resizeFill
    skView.presentScene(menuScene)

}

菜单场景

class MenuScene: SKScene {

    let playButton = SKLabelNode()


    override init(size: CGSize) {
        super.init(size: size)

        backgroundColor = SKColor.white

        playButton.fontColor = SKColor.black
        playButton.text = "play"

        playButton.position = CGPoint(x: size.width / 2, y: size.height / 2)

        addChild(playButton)

    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.location(in: self)

        if playButton.contains(touchLocation) {

            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let difficultyScene = DifficultyScene(size: self.size)
            self.view?.presentScene(difficultyScene, transition: reveal)

        }

    }


}

难度场景

class DifficultyScene: SKScene {

    let easyButton = SKLabelNode()
    let hardButton = SKLabelNode()
    let menuButton = SKLabelNode()


    override init(size: CGSize) {
        super.init(size: size)

        backgroundColor = SKColor.white

        easyButton.fontColor = SKColor.black
        easyButton.text = "easy"

        hardButton.fontColor = SKColor.black
        hardButton.text = "hard"

        menuButton.fontColor = SKColor.black
        menuButton.text = "menu"

        easyButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
        hardButton.position = CGPoint(x: size.width / 2, y: size.height / 2 - easyButton.fontSize * 2)
        menuButton.position = CGPoint(x: size.width / 4 * 3, y: size.height / 4)

        addChild(easyButton)
        addChild(hardButton)
        addChild(menuButton)

    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.location(in: self)

        if easyButton.contains(touchLocation) {
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let gameScene = GameScene(size: self.size, difficulty: easyButton.text!)
            self.view?.presentScene(gameScene, transition: reveal)
        }

        if hardButton.contains(touchLocation) {
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let gameScene = GameScene(size: self.size, difficulty: hardButton.text!)
            self.view?.presentScene(gameScene, transition: reveal)
        }

        if menuButton.contains(touchLocation){
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let menuScene = MenuScene(size: self.size)
            self.view?.presentScene(menuScene, transition: reveal)
        }

    }


}

游戏场景

将此添加到您的 GameScene:

init(size: CGSize, difficulty: String) {
        super.init(size: size)
        gameDifficulty = difficulty
    }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

故事板

或者,您可以使用故事板。在 M.W.E. 中换另一个 S.O。问他们有基本的 "menu" 设置。

在你的情况下,你会做的是:

  • 转到Main.storyboard。
  • 在right-hand工具栏上,找到视图控制器
  • 将view-controller拖入Main.storyboard
  • 点击新建view-controller
  • 单击 right-hand 工具栏上的 - 身份检查器(看起来像名片)
  • 将 Class 更改为 GameViewController
  • 单击左侧层次结构中的视图(在新视图控制器下)
  • 单击身份检查器
  • 将class更改为SKView
  • 单击原始视图控制器
  • 点击身份检查器
  • 将 class 更改为 UIViewController
  • 单击原始 UIViewController 中的视图
  • 单击身份检查器
  • 将 class 更改为 UIView
  • right-hand 侧工具栏底部的查找按钮
  • 将它拖到第一个视图上
  • 右键单击从按钮拖动到第二个视图
  • 在 pop-up 菜单上,在操作 segue 下,单击显示
  • 右键单击从按钮向上拖动,添加水平居中约束
  • 右键单击从按钮向右拖动,添加垂直居中约束

图片