如何根据用户输入更改其物理世界引力后重新加载场景?

How to reload scene after changing its physics world gravity based on user input?

我想允许用户根据他们在重力文本字段中的输入更改重力并将输入从视图控制器传递到 SKScene (PhysicsScene),但我不知道如何重新加载场景以便它使用用户自定义重力而不是其默认值。

view of simulator

class SimulationViewController: UIViewController, UITextFieldDelegate {

    var sceneNode : PhysicsScene?

    @IBOutlet weak var heightTextField: UITextField!
    @IBOutlet weak var massTextField: UITextField!
    @IBOutlet weak var gravityTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        sceneNode = PhysicsScene(size: view.frame.size)
        gravityTextField.delegate = self
        if let view = self.view as! SKView? {
            view.presentScene(sceneNode)
            view.ignoresSiblingOrder = true
            view.showsPhysics = true
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.tag == 0 {
            if let text = textField.text {
                sceneNode?.customGravity = Double(text)!
                print(text)
            }
        }
        return true
    }

请帮忙!谢谢!

您有 2 个选项,在您的 PhysicsScene class

中创建一个 属性
  var customGravity : Double = 0{ didSet { physicsWorld.gravity = {CGPoint(x:0 y:CGFloat(customGravity)}}

或放弃自定义重力并直接设置

 func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.tag == 0 {
            if let text = textField.text {
                sceneNode?.customGravity = physicsWorld.gravity = {CGPoint(x:0 y:CGFloat(Double(text)!)}
                print(text)
            }
        }
        return true
    }

我没有 XCode 可用于检查我的拼写错误,您可能需要解包一些变量