当前位置:  开发笔记 > 编程语言 > 正文

Swift中的主菜单

如何解决《Swift中的主菜单》经验,为你挑选了1个好方法。

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

我使用以下代码:

import SpriteKit

class menuScene: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, 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 {."线程1,断点1.1"

我不完全确定这是什么,但我希望有人可以提供帮助.谢谢!



1> SumNeuron..:
习惯 SKViews

让我们说,就像这个MWE一样,你想要一个菜单​​,一个难度和一个游戏场景.

然后你可以制作一系列自定义SKViews来进行转换.

在此输入图像描述

GameViewController

此代码加载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)

}

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

        }

    }


}

DifficultyScene

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, 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

将此添加到您的GameScene:

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

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

故事板

或者,您可以使用Storyboards.在 针对另一个SO问题的MWE中,他们设置了基本的"菜单".

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

转到Main.storyboard.

在右侧工具栏上,找到视图控制器

将视图控制器拖到Main.storyboard中

单击新的视图控制器

点击 - 右侧工具栏 - 身份检查员(看起来像一张名片)

将Class更改为GameViewController

单击左侧层次结构中的视图(在新视图控制器下)

单击身份检查器

将课程更改为SKView

单击原始视图控制器

单击身份检查器

将类更改为UIViewController

单击原始UIViewController中的视图

点击身份检查员

将班级改为UIView

找到右侧工具栏底部的按钮

将其拖到第一个视图上

右键单击从按钮拖动到第二个视图

在弹出菜单中,在action segue下,单击"显示"

右键单击从按钮向上拖动,添加水平中心约束

右键单击从右侧按钮拖动,垂直添加中心约束

图片

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

推荐阅读
mobiledu2402851373
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有