代码之家  ›  专栏  ›  技术社区  ›  Theresa Le

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

  •  1
  • Theresa Le  · 技术社区  · 7 年前

    我想允许用户根据他们在重力文本字段中的输入更改重力,并将输入从视图控制器传递到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
        }
    

    请帮忙!非常感谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Knight0fDragon    7 年前

    您有2个选项,在PhysicsScene类中创建属性

      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来检查我的打字错误,您可能需要打开一些变量