本文介绍了Swift 中的主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想快速为我的游戏创建一个主菜单.

我正在使用以下代码:

import SpriteKit

class menuScene:SKScene {

//添加开始按钮让 startButton = SKSpriteNode(imageNamed: "playButton")覆盖 func didMove(查看:SKView){//临时背景backgroundColor = SKColor.darkGray//开始按钮startButton.position = CGPoint(x: size.width/2, y: size.height/2)添加孩子(开始按钮)}覆盖 func touchesBegan(_ touches: Set, with event: UIEvent?) {触摸触摸{让 location = touch.location(in: self);//查找触摸位置if atPoint(location) == startButton {如果让场景 = GameScene(fileNamed: "GameScene") {场景.scaleMode = .aspectFill查看!.presentScene(场景,过渡:SKTransition.doorsOpenVertical(withDuration:1))}}}}

}

然而,当我运行它时,如果 atPoint(location) == startButton {,我的应用程序崩溃并突出显示.带有线程 1,断点 1.1"

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

解决方案

Custom SKViews

比方说,像这个

游戏视图控制器

此代码加载 menuScene:

override func viewDidLoad() {super.viewDidLoad()让 menuScene = MenuScene(大小:view.bounds.size)让 skView = 查看为!视窗skView.showsFPS = 真skView.showsNodeCount = 真skView.ignoresSiblingOrder = truemenuScene.scaleMode = .resizeFillskView.presentScene(menuScene)}

菜单场景

class MenuScene: SKScene {让 playButton = SKLabelNode()覆盖初始化(大小:CGSize){super.init(大小:大小)背景颜色 = SKColor.whiteplayButton.fontColor = SKColor.blackplayButton.text = "播放"playButton.position = CGPoint(x: size.width/2, y: size.height/2)添加孩子(播放按钮)}必需的 init(coder aDecoder: NSCoder) {fatalError("init(coder:) 尚未实现")}覆盖 func touchesEnded(_ touches: Set, with event: UIEvent?) {让触摸 = touches.first让 touchLocation = touch!.location(in: self)如果 playButton.contains(touchLocation) {让揭示 = SKTransition.doorsOpenVertical(withDuration: 0.5)让difficultScene = DifficultyScene(大小:self.size)self.view?.presentScene(difficultyScene,过渡:揭示)}}}

困难场景

class DifficultyScene: SKScene {让easyButton = SKLabelNode()让 hardButton = SKLabelNode()让 menuButton = SKLabelNode()覆盖初始化(大小:CGSize){super.init(大小:大小)背景颜色 = SKColor.whiteeasyButton.fontColor = SKColor.blackeasyButton.text = "简单"hardButton.fontColor = SKColor.blackhardButton.text = "硬"menuButton.fontColor = SKColor.blackmenuButton.text = "菜单"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(菜单按钮)}必需的 init(coder aDecoder: NSCoder) {fatalError("init(coder:) 尚未实现")}覆盖 func touchesEnded(_ touches: Set, with event: UIEvent?) {让触摸 = touches.first让 touchLocation = touch!.location(in: self)如果easyButton.contains(touchLocation) {让揭示 = SKTransition.doorsOpenVertical(withDuration: 0.5)让 gameScene = GameScene(大小:self.size,难度:easyButton.text!)self.view?.presentScene(gameScene,transition:reveal)}如果 hardButton.contains(touchLocation) {让揭示 = SKTransition.doorsOpenVertical(withDuration: 0.5)让 gameScene = GameScene(大小:self.size,难度:hardButton.text!)self.view?.presentScene(gameScene,transition:reveal)}如果 menuButton.contains(touchLocation){让揭示 = SKTransition.doorsOpenVertical(withDuration: 0.5)让 menuScene = MenuScene(大小:self.size)self.view?.presentScene(menuScene,过渡:揭示)}}}

游戏场景

将此添加到您的GameScene:

init(size: CGSize, 难度: String) {super.init(大小:大小)游戏难度 = 难度}需要初始化?(编码器aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}

故事板

或者,您可以使用故事板.在

I would like to create a main menu for my game in swift.

I am using the following code:

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

        }
    }
}

}

When I run this however, My app crashes and highlights if atPoint(location) == startButton {. with "Thread 1, Breakpoint 1.1"

Im not entirely sure what this is but I hope someone can help. Thanks!

解决方案

Custom SKViews

Let's say, like this M.W.E., you want a menu, a difficulty, and a game scene.

Then you can make a series of custom SKViews to transition between.

GameViewController

This code loads the 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<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)

        }

    }


}

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

add this to your 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

Alternatively, you can use Storyboards. In the M.W.E. for another S.O. question they have a basic "menu" set up.

In your case, what you would do is:

  • go to Main.storyboard.
  • on the right-hand tool bar, find view controller
  • drag view-controller into Main.storyboard
  • click on the new view-controller
  • click - on the right-hand tool bar - the identity inspector (looks like a business card)
  • change Class to GameViewController
  • click on view within the hierarchy on the left (under the new view controller)
  • click the identity inspector
  • change class to SKView
  • click on the original view controller
  • click on the identity inspector
  • change class to UIViewController
  • click on the view within the original UIViewController
  • click on identity inspector
  • change class to UIView
  • find button at the bottom of the right-hand side tool bar
  • drag it onto the first view
  • right click drag from the button to the second view
  • on the pop-up menu, under action segue, click show
  • right click drag from the button up, add horizontally center constraints
  • right click drag from the button to the right, add vertically center constraints

Images

这篇关于Swift 中的主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:22
查看更多