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

假如想象 提交于 2019-12-01 22:20:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22284211/where-is-the-right-place-to-configure-skscene-content-in-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!