我有一个学校项目来制作一款进展良好的游戏,但我正在努力添加一个后退按钮。我有一个想法,点击单词返回带你进入菜单,但我不知道该怎么做。正常的"按钮"不可用,因为它是从精灵套件场景中返回的。任何建议或帮助将不胜感激。
你来了:
菜单VC.swift:
enum gameType {
case easy
case wobble
case player2
}
var currentGameType: gameType = .easy
var navController = UINavigationController()
var storyBoard = UIStoryboard()
func presentMenuVC() {
let menuVC = storyBoard.instantiateViewController(withIdentifier: "menuVC") as! MenuVC
navController.pushViewController(menuVC, animated: true)
}
class MenuVC : UIViewController {
@IBAction func Player2(_ sender: Any) {
moveToGame(game: .player2)
}
@IBAction func Easy(_ sender: Any) {
moveToGame(game: .easy)
}
@IBAction func Wobble(_ sender: Any) {
moveToGame(game: .wobble)
}
func moveToGame(game: gameType) {
navController = self.navigationController!
storyBoard = self.storyboard!
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController
currentGameType = game
self.navigationController?.pushViewController(gameVC, animated: true)
}
}
新建文件.swift:
class MainMenuButton: SKSpriteNode {
init(text: String, font: String) {
let label = SKLabelNode(text: text)
label.fontName = font
let texture = SKView().texture(from: label)
super.init(texture: texture!, color: .clear, size: texture!.size())
isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
presentMenuVC()
}
required init?(coder aDecoder: NSCoder) { fatalError() }
}
GameScene.swift(添加到底部的didMoveToView功能(:
addChild(MainMenuButton(text: "go back", font: "Chalkduster"))
您可以向SKView
添加普通UIButton
,但这可能不是最干净的解决方案。您可以改为我们在SKLabelNode
上使用触摸功能,例如 touchesBegan
和 touchesEnded
.我以这种方式为SpriteKit
做了一个简单的按钮实现。您可以在GitHub上查看 这里.