1

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

Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject

我的问题是该层没有在应该释放的时候释放。例如,如果我使用以下代码重放关卡:

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

然后内存占用量就会增加。如果我有一个像 menu -> Level 1 -> menu -> Level 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级释放:

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

Box2dLevel 释放:

-(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 释放:

-(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");
4

1 回答 1

0

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

于 2013-09-11T21:44:30.663 回答