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

问题描述:

我想允许用户根据重力文本字段中的输入来改变引力,并将视图控制器的输入传递给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类

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

+0

谢谢你的作品! –

+0

完全没问题 – Knight0fDragon