Cocos2d加载多个动画和纹理时占用大量内存,如何解决
我正在开发一种游戏,在进入游戏场景之前需要加载 27 个纹理 altas(每个 1024 * 1024),
但有时我的游戏崩溃,因为收到内存警告
我知道 27 个纹理 altas 将使用: 4 * 27 * 1024 * 1024 = 108MB 内存 这是一个巨大的数量,但我确实需要在进入游戏之前加载它们。
有办法解决我的问题吗? 任何想法将不胜感激!
顺便提一句: 我使用的是cocos2d 1.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的建议是检查您的设计,以及预加载所有这些纹理的“需要”。我倾向于仅预加载最常用的纹理(动画和静态地图对象)。
例如,我有用于在 16 个字符类的地图上动画行走的纹理。我将“空闲”动画重新组合为 4 个纹理,并预加载它们,因为最初,当士兵进入场景时,它会空闲。移动动画位于单独的纹理中,对于移动中的每个角色类,这些纹理根据行进方向实时加载。当角色停止行走(空闲)时,我从缓存中删除未使用的纹理以及未使用的精灵帧。
另外:还有其他内存管理途径。您可以对某些纹理使用 16 位格式(默认为 RGB88888)。您可以通过转换为压缩的 PVR 格式来获得收益(这又是有损的,但对于某些纹理来说可能没问题)
看看 那里可以详细了解coco中的图片格式,以及与内存消耗的关系(以及加载、渲染速度)。但再次强调,在开始优化之前,请确保除了预加载所有方法之外没有其他选择。
Best suggestion is to review your design, and the 'need' for preloading all these textures. I tend to pre-load only the textures that are most frequently used (animations and static map objects).
For example, I have textures for animating walks on a map for 16 character classes. I regrouped the 'idle' animations in 4 textures, and preload these, because initially, when a soldier enters the scene, it idles. The moving animations are in separate textures that are loaded in real time, as a function of the direction of travel, for each character class in movement. When the characters stop walking (idle), i remove unused textures from the cache, as well as unused sprite frames.
Also: there are other avenues for memory management. You could use a 16 bit format for certain textures (RGB88888 is the default). You may gain by converting to compressed PVR format (once again this is lossy, but could be fine for some textures)
Look here and there to learn more about picture formats in coco, and the relationship to memory consumption (as well as load, rendering speeds). But once again, before you start optimizing, make certain you have no alternative to the pre-load all approach.
使用 jpg 而不是 png 会使其不透明,您可以通过该图像的 alpha 图像使其透明,这将帮助您减小您现在使用的几乎一半的大小。
use jpg instead of png it will make that non transparent you can make that transparent by alpha image of that image it will help you reducing size almost half of you are using now.