Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use sceneDidLoad v didMove(to view:)

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

like image 306
CMR Avatar asked Oct 10 '17 01:10

CMR


1 Answers

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

like image 196
Murphy Avatar answered Oct 21 '22 01:10

Murphy