Cocos2d and iPhone: Parallax Background with multi

2019-08-19 03:43发布

The problem I am trying to solve is:

I have 6 stripes which I need to move at different speed. A texture sheet of 2048*2048 is not enough and to deal with this I splitted the image in two (top and bottom half), so each stripe is exactly 960*640pixels. The general algorithm is to allocate a top and bottom half for each stripe and move them at each frame making sure to reposition them at the top of the screen when they exit the user's view. My class implementation, a direct modification of ParallaxBackground in the ShootEmUp example from this book, is giving too many memory warnings when run and analyzed using Instruments. See analysis below:

OpenGL analysis: OpenGL analysis

Activity monitor: Activity Monitor analysis

What concerns me is the high number of memory warnings in both analysis (24 and 5 respectively).

EDIT: Below you can find a comment which explains the solution

3条回答
贪生不怕死
2楼-- · 2019-08-19 04:16

Remember that Cocos2D always saves images with a width/height being a power of 2. So if your image is 960x640 pixels it'll use memory as if the image is 1024x1024 pixels.

Also remove textures you no longer need (and when outofmemory gets called).

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

You can also use images in a lower quality to save memory.

[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444];

Whenever you need to load higher quality images or gradients you can put it back.

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
查看更多
做个烂人
3楼-- · 2019-08-19 04:20

2048x2048 is maximum possible size of texture for new devices. you can read about it in Apple OpenGL ES Programming Guide

查看更多
时光不老,我们不散
4楼-- · 2019-08-19 04:25

Following the comment-suggestion to my question by LearnCocos2d the correct answer-solution was to simply reboot the device.. (see his comment above).

Thanks!

查看更多
登录 后发表回答