c++ - Cocos2d replaceScene 不释放旧场景

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

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

Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject

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

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

然后内存占用量就会增加。如果我有一个像菜单这样的圆圈,同样的事情也会发生 -> Level 1 -> menu -> Level 1 并且我的应用程序在一段时间后因为内存不足而崩溃。我跟踪了 dealloc使用 NSLogs 的方法,但是当我替换场景时,每个 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");

最佳答案

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

关于c++ - Cocos2d replaceScene 不释放旧场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440662/

相关文章:

ios - UIGestureRecognizerDelegate 来管理手势——我可以解除触摸分配吗?

c++ - SDL + SDL_ttf : Transparent blended text?

c++ - 在 GDI+ 中以特定角度绘制字符串时 AntiAlias 失败

ios 7 drawViewHierarchyInRect :afterScreenUpdates: black screen on iPad only

ios - 无法安装 Codename 一个 iOS 调试应用程序

iOS:举起聆听

c++ - 如何在 OpenGL 中设置顶点的不透明度?

c++ - Qt、QTextStream - 将字符/字符串输入到 int 中

iphone - -viewDidLoad 未在子类 UIViewController 中调用

ios - 将应用程序提交到 iTunes Connect 时排除 iPod