在SpriteKit中配置SKScene内容的正确位置在哪里? [英] Where is the right place to configure SKScene content in SpriteKit?

查看:98
本文介绍了在SpriteKit中配置SKScene内容的正确位置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用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:. 在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 .使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆