Cocos2d:大背景导致内存泄漏和崩溃

时间:2013-12-03 13:51:39

标签: ios memory-management background cocos2d-iphone

我正在开发一个背景很大的游戏,背景由十个图像组成。当
游戏塔越高,背景向下滚动。我正在以这种方式构建背景:

layer.backgroundLayer = [CCLayer node];
layer.backgroundLayer.anchorPoint = ccp(0, 0);
CCSprite *background = nil;
CCTexture2D *mytexture = nil;
NSString *temp = nil;
UIImage *myimage = nil;
for (int i=0; i < 9; i++) {

    temp = [NSString stringWithFormat:@"bg_0%d.jpg",10-i];
    if (i == 0) {
        temp = [NSString stringWithFormat:@"bg_%d.jpg",10-i];
    }
    myimage = [UIImage imageNamed:temp];
    mytexture = [[CCTexture2D alloc] initWithImage:myimage];

    background = [CCSprite spriteWithTexture:mytexture rect:CGRectMake(0,0,640,2000)];
    background.anchorPoint = ccp(0.0,0.0);
    [background setPosition:CGPointMake(0.0,1000.0 * i-4)];
    [background setScaleX: 0.5];
    [background setScaleY: 0.5];

    //[background setPosition:CGPointMake(0.0,0.0)];

    [layer.backgroundLayer addChild:background];

}
[scene addChild:layer.backgroundLayer z:-1];

滚动bg 5或6次后游戏崩溃。有没有办法优化bg。 PS:一张bg图像的大小为235 ko。

创建时没有内存问题。滚动时内存的使用变得很重要。

0 个答案:

没有答案