Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the right place to configure SKScene content in SpriteKit?

Is it ok to configure (position sprites, add visible nodes etc) an SKScene's content in init method?

Where is the right place for such things: init? didMoveToView? something else?

like image 973
AndrewShmig Avatar asked Feb 14 '23 14:02

AndrewShmig


1 Answers

didMoveToView: is called every time the scene is presented by an SKView. Pros of positioning and adding sprites in didMoveToView: You can initialise many views without them grabbing a lot of memory. Cons: If you remove a view and then add it again didMoveToView: is called again. This means that you need to be sure to reset your scene in the beginning of didMoveToView: (only if you intend to remove and add again).

init is called when you initialise the SKScene. Pros of using init for positioning and adding sprites: It is only called once, and everything will be ready once you present it on the scene. If you need to preload scenes for quick switching this might be handy. Cons: Each scene will take up the memory it needs to perform all the adding of sprites when init'ed and not when its shown.

Personally, I prefer to do everything in the init method.

like image 183
Theis Egeberg Avatar answered Mar 22 '23 23:03

Theis Egeberg