我已经搜索了有关如何将UIKit元素添加到SpriteKit的更新指南,但我还没有发现任何有用的内容。具体来说,我想在我的游戏设置屏幕上添加一个滑块,在其上方的标签中显示滑块的当前值。此外,如果滑块有效,这是否意味着可以以相同的方式成功添加任何UIKit元素?
class Settings: SKScene {
//MARK: - Properties
var settingsLabel = SKLabelNode()
var backLabel = SKLabelNode()
var shipType1 = SKSpriteNode()
var shipType2 = SKSpriteNode()
var shipType3 = SKSpriteNode()
var shipType4 = SKSpriteNode()
var shipType5 = SKSpriteNode()
var shipArr: [SKSpriteNode] = []
var starCount = SKLabelNode()
var starCountLogo = SKSpriteNode()
var resetStars = SKLabelNode()
var resetHighScores = SKLabelNode()
//let slider: UISlider = UISlider(frame: CGRect(x: 0, y: -450, width: 200, height: 50)
//MARK: - didMove and willMove
override func didMove(to view: SKView) {
settingsLabel = self.childNode(withName: "settingsLabel") as! SKLabelNode
backLabel = self.childNode(withName: "backLabel") as! SKLabelNode
shipType1 = self.childNode(withName: "shipType1") as! SKSpriteNode
shipType2 = self.childNode(withName: "shipType2") as! SKSpriteNode
shipType3 = self.childNode(withName: "shipType3") as! SKSpriteNode
shipType4 = self.childNode(withName: "shipType4") as! SKSpriteNode
shipType5 = self.childNode(withName: "shipType5") as! SKSpriteNode
starCount = self.childNode(withName: "starCount") as! SKLabelNode
starCountLogo = self.childNode(withName: "starCountLogo") as! SKSpriteNode
resetStars = self.childNode(withName: "resetStars") as! SKLabelNode
resetHighScores = self.childNode(withName: "resetHighScores") as! SKLabelNode
shipArr = [shipType1, shipType2, shipType3, shipType4, shipType5]
switch shipType {
case .Spaceship1:
resetTextures(node: shipType1, name: "Spaceship 1 Border")
case .Spaceship2:
resetTextures(node: shipType2, name: "Spaceship 2 Border")
case .Spaceship3:
resetTextures(node: shipType3, name: "Spaceship 3 Border")
case .Spaceship4:
resetTextures(node: shipType4, name: "Spaceship 4 Border")
case .Spaceship5:
resetTextures(node: shipType5, name: "Spaceship 5 Border")
}
let stars: Int = UserDefaults.standard.value(forKey: "starNum") as! Int
starCount.text = String(stars)
}
//MARK: - Touch Handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if backLabel.contains(location) {
let transition: SKTransition = SKTransition.fade(withDuration: 0.5)
let scene: SKScene = Menu(fileNamed: "Menu")!
scene.scaleMode = .aspectFill
self.view?.presentScene(scene, transition: transition)
}
else if resetStars.contains(location) {
resetStars.run(pulseText)
UserDefaults.standard.set(0, forKey: "starNum")
starCount.text = "0"
starNum = 0
}
else if resetHighScores.contains(location) {
resetHighScores.run(pulseText)
changeLeaderboard(whichScore: 0, toScore: 0)
}
else if shipType1.contains(location) {
resetTextures(node: shipType1, name: "Spaceship 1 Border")
shipType = .Spaceship1
}
else if shipType2.contains(location) {
resetTextures(node: shipType2, name: "Spaceship 2 Border")
shipType = .Spaceship2
}
else if shipType3.contains(location) {
resetTextures(node: shipType3, name: "Spaceship 3 Border")
shipType = .Spaceship3
}
else if shipType4.contains(location) {
resetTextures(node: shipType4, name: "Spaceship 4 Border")
shipType = .Spaceship4
}
else if shipType5.contains(location) {
resetTextures(node: shipType5, name: "Spaceship 5 Border")
shipType = .Spaceship5
}
}
}
//MARK: - Extra Functions
func fadeIn(node: SKNode, duration: Double) {
let fadeIn = SKAction.fadeIn(withDuration: duration)
node.run(fadeIn)
}
func fadeOut(node: SKNode, duration: Double) {
let fadeOut = SKAction.fadeOut(withDuration: duration)
node.run(fadeOut)
}
func changeTexture(node: SKSpriteNode, name: String) {
node.texture = SKTexture(image: UIImage(named: name)!)
}
func resetTextures(node: SKSpriteNode, name: String) {
var i = 1
for spriteNode in shipArr {
changeTexture(node: spriteNode, name: "Spaceship\(i)")
i+=1
}
changeTexture(node: node, name: name)
}
}
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'Menu.sks'
if let scene = SKScene(fileNamed: "Menu") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}