iOS点击标签后是否可以切换场景?
Is it possible to change scene after tapping a label in iOS?
我有一个学校项目要制作一款运行良好的游戏,但我很难添加后退按钮。我想点击“返回”一词将您带到菜单,但我不知道该怎么做。正常的 "button" 不可用,因为它正在从 sprite 套件场景返回。任何建议或帮助表示赞赏。
您可以将普通 UIButton
添加到 SKView
,但这可能不是最干净的解决方案。您可以改为使用 SKLabelNode
上的触摸功能,例如 touchesBegan
和 touchesEnded
。我用这种方式为 SpriteKit
做了一个简单的按钮实现。您可以在 GitHub here.
上查看
给你:
MenuVC.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)
}
}
NewFile.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"))
我有一个学校项目要制作一款运行良好的游戏,但我很难添加后退按钮。我想点击“返回”一词将您带到菜单,但我不知道该怎么做。正常的 "button" 不可用,因为它正在从 sprite 套件场景返回。任何建议或帮助表示赞赏。
您可以将普通 UIButton
添加到 SKView
,但这可能不是最干净的解决方案。您可以改为使用 SKLabelNode
上的触摸功能,例如 touchesBegan
和 touchesEnded
。我用这种方式为 SpriteKit
做了一个简单的按钮实现。您可以在 GitHub here.
给你:
MenuVC.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)
}
}
NewFile.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"))