2 xcode ios sprite-kit skspritenode swift
我需要帮助在游戏场景中呈现警报视图.我目前正在努力这样做,因为GameScene.Swift不是标准的ViewController.如果它有帮助我需要这样做,因为我需要用户输入一个值,该值用作我的游戏中的球Sprite Kit节点的坐标.输入只是一个标准整数,所以这不是问题.关于如何通过警报视图执行此操作的任何其他想法也是受欢迎的.
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    let view = self.view as! SKView
    if view.scene == nil {
        view.showsFPS = false
        view.showsNodeCount = false
        let gameScene = GameScene(size: view.bounds.size)
        gameScene.scaleMode = SKSceneScaleMode.AspectFill
        view.presentScene(gameScene)
    }
}
那是在GameViewController文件中
    var vc : GameViewController!
override init(size: CGSize) {
    super.init(size: size)
    let alertController = UIAlertController(title: "Bll Starting Position", message: "Please Enter a X Coordinate Value IN Range 0 to 345 ", preferredStyle: .Alert)
    alertController.addTextFieldWithConfigurationHandler { (textField) in
        textField.placeholder =  "Value Must Be In Range 0 To 345"
        textField.autocapitalizationType = UITextAutocapitalizationType.None
        textField.autocorrectionType = UITextAutocorrectionType.No
        textField.clearsOnBeginEditing = true
        textField.clearsOnInsertion = true
        textField.clearButtonMode = UITextFieldViewMode.Always
        let cancelBtn = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        let confirmBtn = UIAlertAction(title: "Confirm", style: .Default, handler: { (confirmView) in
            if let field = alertController.textFields![0] as? UITextField {
            }
        })
        alertController.addAction(confirmBtn)
        alertController.addAction(cancelBtn)
        self.vc.presentViewController(alertController, animated: true, completion: nil)
    }
谢谢
你可以直接从SKScenes展示UIAlertControllers,只需在rootViewController上显示它们,这可能是最好的展示它们的地方.
view?.window?.rootViewController?.present...
一般来说,它不是在SKScenes中引用GameViewController的最佳实践,而我实际上从未达到我被迫这样做的地步.NSNotificationCenter,委派或协议扩展是更好的方法.
我实际上使用了一个助手,我使用Swift 2的协议扩展来制作警报.
只需创建一个新的.swift文件并添加此代码即可
import SpriteKit
protocol Alertable { }
extension Alertable where Self: SKScene {
    func showAlert(withTitle title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
        alertController.addAction(okAction)
        view?.window?.rootViewController?.present(alertController, animated: true)
    }
    func showAlertWithSettings(withTitle title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
        alertController.addAction(okAction)
        let settingsAction = UIAlertAction(title: "Settings", style: .default) { _ in
            guard let url = URL(string: UIApplicationOpenSettingsURLString) else { return }
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
        alertController.addAction(settingsAction)
        view?.window?.rootViewController?.present(alertController, animated: true)
    }
}
现在,在您的场景中,您需要显示符合协议的警报
class GameScene: SKScene, Alertable {
} 
并调用方法
showAlert(withTitle: "Alert title", message: "Alert message")
好像他们是场景本身的一部分.
希望这可以帮助