问题描述
我有一个关于 Cocos2D 的问题.我的游戏中有 2 个场景,场景中都有很多精灵.当我替换一个场景并加载另一个场景时发生了什么?是所有精灵都自动删除还是正在记忆中,当我在场景的dealloc中释放时会释放?
I have a question about Cocos2D. I have 2 scenes in my game both have a lot of sprites on the scene. What happened when I replace the one scene and load another? Are all sprites removed automatically or it is taking memories and will be release when I release in dealloc of the scene?
我无法理解在这种情况下内存是如何管理的,所以任何人都可以向我解释一下,还有一件事是这对我们在场景中加载精灵时保持精灵很重要.
I am unable to understand of this thing how the memory manage in this condition, so can any one explain me that thing and one thing more is this is important for us to make the sprite retain when we load it on the scene.
推荐答案
调用replaceScene的那一刻,两个场景都会在内存中存在一小段时间!如果您使用 CCSceneTransition,那么在过渡期间两个场景都将在内存中.
The moment you call replaceScene both scenes will exist in memory for a short time period! If you use a CCSceneTransition then both scenes will be in memory for the duration of the transition.
流程如下:
- 场景 A 正在运行
- 用sceneB调用CCDirector replaceScene
- 场景 B 已初始化,可能会加载精灵等
- 场景 A 被释放并释放内存
您可以在场景中实现 -(void) cleanup
方法以在更改场景之前删除其大部分内容,并且您可能希望将新内容加载到 -(void) onEnter { [超级 onEnter];}
方法.
You can implement the -(void) cleanup
method in a scene to remove most of its content before changing the scene, and you may want to defer loading new content to the -(void) onEnter { [super onEnter]; }
method.
就我个人而言,在这种情况下,我发现更好的方法是加载"场景,即介于两个场景之间的场景(它甚至可能在几毫秒内都不处于活动状态).这使第一个场景有时间在下一个场景初始化之前被释放.
Personally I find the better approach in this scenario a "loading" scene, a scene that comes in between the two scenes (it may not even be active for more than a few milliseconds). This gives the first scene time to be deallocated before the next scene is initialized.
这篇关于Cocos2D 场景内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!