,可以通过以下步骤完成:
private var pauseMenu: SKNode?
override func didMove(to view: SKView) {
// 初始化暂停菜单节点
pauseMenu = SKNode()
// 添加暂停菜单节点到场景中
addChild(pauseMenu!)
}
func showPauseMenu() {
// 创建菜单背景
let menuBackground = SKSpriteNode(color: .gray, size: CGSize(width: 200, height: 300))
menuBackground.position = CGPoint(x: size.width/2, y: size.height/2)
pauseMenu?.addChild(menuBackground)
// 创建继续游戏按钮
let resumeButton = SKLabelNode(text: "Resume")
resumeButton.position = CGPoint(x: size.width/2, y: size.height/2 + 50)
pauseMenu?.addChild(resumeButton)
// 创建重新开始按钮
let restartButton = SKLabelNode(text: "Restart")
restartButton.position = CGPoint(x: size.width/2, y: size.height/2)
pauseMenu?.addChild(restartButton)
// 创建返回主菜单按钮
let mainMenuButton = SKLabelNode(text: "Main Menu")
mainMenuButton.position = CGPoint(x: size.width/2, y: size.height/2 - 50)
pauseMenu?.addChild(mainMenuButton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if pauseButton.contains(location) {
showPauseMenu()
}
}
}
至此,你已经成功在Xcode SpriteKit中创建了一个暂停菜单。你可以根据需要自定义菜单的样式和功能。
领取专属 10元无门槛券
手把手带您无忧上云