gpt4 book ai didi

swift - Swift 中的主菜单

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:08 24 4
gpt4 key购买 nike

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

我正在使用以下代码:

import SpriteKit

菜单场景类: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 {.使用“线程 1,断点 1.1”

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

最佳答案

自定义 SKViews

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

然后您可以制作一系列自定义 SKViews 以在它们之间进行转换。

enter image description here

游戏 View Controller

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

Storyboard

或者,您可以使用 Storyboard。在M.W.E.对于另一个 S.O.问题是他们设置了基本的“菜单”。

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

  • 转到 Main.storyboard。
  • 在右侧工具栏上,找到 View Controller
  • 将 view-controller 拖到 Main.storyboard 中
  • 点击新的 View Controller
  • 点击 - 在右侧工具栏上 - 身份检查器(看起来像一张名片)
  • 将类更改为 GameViewController
  • 单击左侧层次结构中的 View (在新 View Controller 下)
  • 点击身份检查器
  • 将类更改为 SKView
  • 点击原来的 View Controller
  • 点击身份检查员
  • 将类更改为 UIViewController
  • 单击原始 UIViewController 中的 View
  • 点击身份检查员
  • 将类更改为 UIView
  • 右侧工具栏底部的查找按钮
  • 将它拖到第一个 View 上
  • 右键单击从按钮拖动到第二个 View
  • 在弹出菜单中,在 action segue 下,单击 show
  • 右键单击从按钮向上拖动,添加水平居中约束
  • 右击从按钮向右拖动,添加垂直居中约束

图片

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

关于swift - Swift 中的主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41665676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com