在 SpriteKit 中配置 SKScene 内容的正确位置在哪里? [英] Where is the right place to configure SKScene content in SpriteKit?
问题描述
是否可以在 init 方法中配置(定位精灵、添加可见节点等)SKScene 的内容?
Is it ok to configure (position sprites, add visible nodes etc) an SKScene's content in init method?
这些东西的正确位置在哪里:init?didMoveToView?还有什么?
Where is the right place for such things: init? didMoveToView? something else?
推荐答案
didMoveToView: 每次由 SKView 呈现场景时都会调用.在 didMoveToView 中定位和添加精灵的优点:您可以初始化许多视图而不会占用大量内存.缺点:如果您删除一个视图,然后再次添加它, didMoveToView: 会再次被调用.这意味着您需要确保在 didMoveToView 的开头重置场景:(仅当您打算删除并再次添加时).
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 在您初始化 SKScene 时被调用.使用 init 定位和添加精灵的优点:它只被调用一次,一旦你将它呈现在场景中,一切就准备好了.如果您需要预加载场景以进行快速切换,这可能会很方便.缺点:每个场景都会占用它在初始化时而不是在显示时执行所有添加精灵所需的内存.
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.
就个人而言,我更喜欢在 init 方法中完成所有操作.
Personally, I prefer to do everything in the init method.
这篇关于在 SpriteKit 中配置 SKScene 内容的正确位置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!