Hunter Allen Hunter Allen - 1 year ago 88
Swift Question

bad access physics field crashes application SWIFT, XCODE

Hey I have a world/Scene with a man in it so I decided to make a physics field to act as gravity. so I'm able to code it in no problem using swift but then I ran the app and it keeps giving me bad access. With out these lines of code my app/scenekit game runs smoothly any help. Please see the image attached Thanks

enter image description here


// I declared field owner up at the top here
class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{

var fieldOwner = SCNNode()

override func viewDidLoad() {

//Force field to act as Gravity

fieldOwner = SCNNode()
fieldOwner.position = SCNVector3(x: 0, y: -5, z: -5)

let field: SCNPhysicsField = SCNPhysicsField.radialGravityField()
field.halfExtent = SCNVector3Make(100, 100, 100)
field.minimumDistance = 20.0
field.falloffExponent = 0

self.fieldOwner.physicsField!.strength = 0.0
self.fieldOwner.physicsField = field


Answer Source

It looks like you are accessing self.fieldOwner.physicsField before setting it. Try setting the strength on your local field variable instead:


self.fieldOwner.physicsField!.strength = 0.0


field.strength = 0.0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download