Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

init() vs didMove vs sceneDidLoad in SpriteKit

Tags:

ios

sprite-kit

I understand that there are 3 ways to create a scene in SpriteKit, init(), didMove, and sceneDidLoad.
But I can't understand to separate the 3 ways. Reading other questions I understood the order to call is init -> SceneDidLoad -> didMove.

How can I use them to use effectively?

like image 225
K.K.D Avatar asked Oct 27 '25 08:10

K.K.D


1 Answers

You're right about the order those functions are called in. But only init(size:) actually creates a scene.

init(size:) initializes a new scene object with the given CGSize as its bounds. Anything that must be set up prior to the scene becoming visible should happen here. That's the important bit because a newly initialized scene isn't visible to the user until it's presented by a view.

sceneDidLoad() is called as a result of init(size:) and can be used to do any more setup required before the scene is presented. init(size:) can be called from wherever you want to make a new scene, but sceneDidLoad() happens in the scene itself. This is useful for any setup that you want all scenes of this class to use.

didMove(to:) is different because it doesn't have to do with the initialization. This function is called when the scene is presented by a view. Basically, when it becomes visible to the user. UI adjustments and layout for elements inside the scene are typically handled here.

like image 72
guard working else panic Avatar answered Oct 29 '25 22:10

guard working else panic