Can someone please help me understand the difference between sceneDidLoad and didMove(to view:) in a GameScene? I realize that didMove(to view:) is called once the scene is presented. While sceneDidLoad is called once the scene is initialized. So its logical order is sceneDidLoad first, then didMove(to view:) later (right?)
With that said, I am trying to create a bouncing ball using the following:
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody = borderBody
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
let testBall = SKShapeNode(circleOfRadius: 20)
self.addChild(testBall)
testBall.physicsBody = SKPhysicsBody(circleOfRadius:
testBall.frame.size.width/2)
testBall.physicsBody!.restitution = 1.0
testBall.physicsBody!.friction = 0.0
testBall.physicsBody!.angularDamping = 0.0
testBall.physicsBody!.linearDamping = 0.0
testBall.physicsBody!.applyImpulse(CGVector(dx: 10.0, dy: 10.0))
By overriding either sceneDidLoad OR didMove, I get the same intended result. I fail to understand which is the 'smarter' or best practice method and why?
Thanks!
C
The order of call is : SceneDidLoad and DidMove
SceneDidLoad : is called right after scene initialization or decoding or scene behavior here You can create your instance variables initialized views and nodes. be careful you can not add your new views because the scene does not know the main view yet but you can add your nodes to the scene
DidMove : here the scene knows the view. you also build all your scenes and the behavior.
So for performance gain issues can use SceneDidLoad to initialize things before the scene is presented to view.
https://developer.apple.com/documentation/spritekit/skscene/1645216-scenedidload https://developer.apple.com/documentation/spritekit/skscene/1519607-didmove
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With