我想允许用户根据他们在重力文本字段中的输入来更改重力,并将输入从 View Controller 传递到 SKScene (PhysicsScene),但我不知道如何重新加载场景以便它使用自定义重力由用户而不是其默认值。
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
}
求助!谢谢!
最佳答案
你有两个选择,在你的 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 来检查我的拼写错误,您可能需要解包一些变量
关于swift - 如何根据用户输入更改其物理世界引力后重新加载场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977417/