Cocos2D场景内存管理 [英] Cocos2D scene memory management

查看:136
本文介绍了Cocos2D场景内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Cocos2D的问题。我有两个场景在我的游戏中都有很多精灵在现场。当我更换一个场景并加载另一个场景时发生了什么?所有的sprite是自动删除还是正在记忆,并将释放,当我释放在场景的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?

我无法理解这个东西如何内存管理在这种情况下,所以任何一个解释我的东西,一件事是,这是重要的,我们使sprite保留当我们加载它的场景。

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正在运行

  • 调用CCDirector replaceScene with sceneB

  • sprite等

  • 场景A已释放并释放内存

  • scene A is running
  • call CCDirector replaceScene with sceneB
  • scene B is initialized, may load sprites etc
  • scene A is deallocated and memory released

您可以实现 - (void)cleanup 方法在一个场景中删除大部分内容,然后改变场景,你可能想推迟加载新内容到 - (void )onEnter {[super 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场景内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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