Cocos2d replaceScene没有发布旧场景

时间:2013-08-26 09:16:03

标签: c++ iphone ios objective-c cocos2d-iphone

我有一个box2d-cocos2d游戏,有几个级别。我的关卡是使用从另一个类继承的Box2d功能的图层。例如,第一层看起来像:

Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject

我的问题是该层在应该发布时不会释放。例如,如果我使用此代码重放Level:

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5f scene:[[self class] node]]];

然后内存占用量增加。如果我有一个像菜单一样的圆圈,也会发生同样的事情 - > 1级 - >菜单 - > 1级和我的应用程序在一段时间后崩溃,因为内存不足。我使用NSLogs跟踪dealloc方法,但是当我替换场景时调用每个dealloc方法。 我的日志看起来像:

*** DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.089 Myapp[6334:907] 
***BOX2DLEVEL DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.093 Myapp[6334:907] ***BOX2DLAYER DEALLOC ***
<Level1Layer = 0x1cdcfe70 | Tag = -1> 

我真的卡住了,因为在iPhone 4播放3级后应用程序崩溃了。 我怎么解决这个问题?任何解决方案和指针都表示赞赏。

1级dealloc:

-(void)dealloc{
    NSLog(@"\n*** DEALLOC ***%@",self);
    [self removeAllChildrenWithCleanup:YES];
    [super dealloc];
}

Box2dLevel dealloc:

-(void) dealloc{
    NSLog(@"\n***BOX2DLEVEL DEALLOC ***%@",self);
    [self removeChild:_label cleanup:YES];
    [self removeChild:labelForCoins cleanup:YES];
    [self removeChild:labelForTime cleanup:YES];
    [self removeChild:freezeCountDown cleanup:YES];
    [self removeChild:freezedMask cleanup:YES];
    [self removeChild:_backGround cleanup:YES];
    [self removeChild:darkLayer cleanup:YES];
    [self removeChild:flashlayer cleanup:YES];
    [self removeChild:skillsMenu cleanup:YES];
    [arrayForObjects release];
    [skillsMenuArray release];
    [super dealloc];
}

Box2dLayer dealloc:

-(void) dealloc
{
    NSLog(@"***BOX2DLAYER DEALLOC ***\n%@",self);
    if (debugDraw) {
        delete debugDraw;
        debugDraw = NULL;
    }
    if (mouseJoint) {
        world->DestroyJoint(mouseJoint);
        mouseJoint = NULL;
    }
    if (groundBody) {
        world->DestroyBody(groundBody);
        groundBody = NULL;
    }
    if (referenceBody) {
        world->DestroyBody(referenceBody);
        referenceBody = NULL;
    }
    if (bombSensor) {
        world->DestroyBody(bombSensor);
        bombSensor = NULL;
    }
    if (laserSensor) {
        world->DestroyBody(laserSensor);
        laserSensor = NULL;
    }
    if (_contactListener) {
        free(_contactListener);
        _contactListener = NULL;
    }
    [self removeChild:_obj cleanup:YES];
    [super dealloc];
    NSLog(@"\n***Box2dLayer superclass deallocated");

1 个答案:

答案 0 :(得分:0)

尝试使用[Level1layer scene]代替[[self class] node](示例项目也这样做)