0

编辑: 我在前一个场景的 dealloc 和 cleanup 方法中添加了以下代码(我从中调用 replaceScene 但它没有任何效果。即使在创建 InstructionScene 后的 1/2/5 秒后内存仍然包含来自前一个场景的资产。强制删除这些资产的唯一方法是在创建场景后 0.1f 秒(通过回调)将它们从新场景中删除。这有点奇怪。

下面是之前场景清理和daelloc方法的代码:

-(void) cleanup
{
    CCLOG(@"");
    CCLOG(@"");
    CCLOG(@"PlanetSelection Menu cleanup");
    CCLOG(@"");
    [super cleanup];
    [planetLayer removeAllChildrenWithCleanup:YES];
    [self removeAllChildrenWithCleanup: YES];

    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:textureFileName];
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [CCAnimationCache purgeSharedAnimationCache];
}


-(void) dealloc
{
    CCLOG(@"Dealloc gets caled");
    [CCAnimationCache purgeSharedAnimationCache];
    [[CCDirector sharedDirector] purgeCachedData];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:textureFileName];
}

原始问题:

我的游戏中有几个场景,到目前为止,我在每个场景的开头使用了以下代码来删除以前存储的纹理。但是,在某些情况下这不起作用:当我用没有精灵且只有一些从字体图像表创建的标签的新场景(我们称之为 B)替换场景(我们称之为 A)时。

[[CCDirector sharedDirector] replaceScene: [InstructionsScene sceneWithLevelName:FIRST_LEVEL]];

新对象的创建速度确实太快,因为以下调用似乎没有任何效果:

-(id) initWithLevelName:(LevelName)name
{
    if ((self = [super init]))
    {
        //Remove stuff from previous scene
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; //Not really necessary


        //Use these
        [[CCTextureCache sharedTextureCache] removeUnusedTextures]; //Not really needed
        [[CCTextureCache sharedTextureCache] removeAllTextures];
        [[CCDirector sharedDirector] purgeCachedData];
        [CCAnimationCache purgeSharedAnimationCache];
        ....
     }
}

在调用替换场景方法的那一刻,两个 CCLayer (CCScane) 对象同时处于活动状态。然而,前一个场景的纹理不会被移除。如果在指令场景中添加和使用精灵表,则相同的代码可以完美运行。对此的一个调整是使用对选择器的回调来删除 0.1f 之后的所有纹理,但这不是很优雅和平滑:

 [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:0.1f] two:[CCCallFunc actionWithTarget:self selector:@selector(removeStuffFromPreviousScene)]]];

这是一个已知的问题?它可能会导致潜在的崩溃。

我在这里粘贴代码以确保您可以尝试一下:

//
//  InstructionsScene.h
//
//  Created by mm24 on 09/09/13.
//  Copyright 2013 mm24. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CommonEnumerations.h"

@interface InstructionsScene : CCLayer {

    LevelName levelName;
    float startTime;

    CCLabelBMFont * levelNameTitle;
    CCLabelBMFont * levelSubtitle;

    CCLabelBMFont * instructionsHeader;
    CCLabelBMFont * instructions;
}

+(id)sceneWithLevelName:(LevelName)name;
@end


//
//  InstructionsScene.m
//
//  Created by mm24 on 09/09/13.
//  Copyright 2013 mm24. All rights reserved.
//

#import "InstructionsScene.h"
#import "ShooterScene.h"
#import "AppDelegate.h"
#import "mach/mach.h"


@implementation InstructionsScene

+(id)sceneWithLevelName:(LevelName)name
{
    CCScene * scene = [CCScene node];
    InstructionsScene * layer = [[self alloc] initWithLevelName:name];

    [scene addChild:layer];
    return scene;
}


-(id) initWithLevelName:(LevelName)name
{
    if ((self = [super init]))
    {
        //Remove stuff from previous scene
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];


        //Use these
        [[CCTextureCache sharedTextureCache] removeUnusedTextures];
        [[CCTextureCache sharedTextureCache] removeAllTextures];
        [[CCDirector sharedDirector] purgeCachedData];
        [CCAnimationCache purgeSharedAnimationCache];

        //Try out and use it. Not compulsory
        [self removeAllChildrenWithCleanup: YES];

        CCLOG(@"init with level name");
        levelName = name;
        startTime = 10.0f;

        levelNameTitle = [CCLabelBMFont labelWithString:@"Title" fntFile:@"bitmapFontTest.fnt"];
        levelNameTitle.position = CGPointMake(160.0f, 420.0f);
        levelNameTitle.anchorPoint = CGPointMake(0.5f, 0.5f);
        levelNameTitle.scale = 1.3f;
        [self addChild:levelNameTitle z:1] ;

        levelSubtitle = [CCLabelBMFont labelWithString:@"Subtitle" fntFile:@"bitmapFontTest.fnt"];
        levelSubtitle.position = CGPointMake(160.0f, 400.0f);
        levelSubtitle.anchorPoint = CGPointMake(0.5f, 0.5f);
        levelSubtitle.scale = 0.7f;
        [self addChild:levelSubtitle z:1] ;

        instructionsHeader   = [CCLabelBMFont labelWithString:@" Instructions " fntFile:@"bitmapFontTest.fnt"];
        instructionsHeader.position = CGPointMake(160.0f, 240.0f);
        instructionsHeader.anchorPoint = CGPointMake(0.5f, 0.5f);
        instructionsHeader.scale = 0.7f;
        [self addChild:instructionsHeader z:1] ;

        instructions  = [CCLabelBMFont labelWithString:@"Press any key" fntFile:@"bitmapFontTest.fnt"];
        instructions.position = CGPointMake(160.0f, 200.0f);
        instructions.anchorPoint = CGPointMake(0.5f, 0.5f);
        instructions.scale = 0.7f;
        [self addChild:instructions z:1] ;

        [[CCDirector sharedDirector] resume];


     //   [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:0.1f] two:[CCCallFunc actionWithTarget:self selector:@selector(removeStuffFromPreviousScene)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:1.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:2.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:5.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self callBackReplace];


    }
    return self;
}

-(void) removeStuffFromPreviousScene
{
    //Use these
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];


    //Use these
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCTextureCache sharedTextureCache] removeAllTextures];
    [[CCDirector sharedDirector] purgeCachedData];
    [CCAnimationCache purgeSharedAnimationCache];

}

-(void) report_memory {
    CCLOG(@"");
    CCLOG(@"");
    CCLOG(@"InstructionScene info:");
    CCLOG(@"");

    [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];
    struct task_basic_info info;

    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &size);
    if( kerr == KERN_SUCCESS ) {
        NSLog(@"Memory in use (in bytes): %u", info.resident_size);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

-(void) nextSuggestionPressed
{
    [self stopAllActions];
    [self callBackReplace];
}

-(void) callBackReplace 
{
    [self runAction:[CCSequence actions:
                     [CCDelayTime actionWithDuration:startTime] ,
                     [CCCallFunc actionWithTarget:self selector:@selector(replaceWithShooterScene)],
                      nil]
     ];
}


-(void) replaceWithShooterScene
{
      [[CCDirector sharedDirector] replaceScene:[ShooterScene sceneWithLevelName:levelName]];
}


@end
4

1 回答 1

1

由于您在已经运行的场景中初始化新场景,因此两个场景同时处于活动状态。如果您尝试在新场景的初始化期间移除“未使用”资源,则不会发生任何事情(或不会发生太多),因为现有场景仍在使用这些资源。

你只能做两件事:

  • 两个场景之间的加载场景,以允许在下一个场景初始化之前释放第一个场景
  • 在场景解除分配期间卸载所有未使用的资产,而不是在初始化新资产时

如果两个场景都使用了很多独特的资源,加载场景是最好的方法,因此同时在内存中可能会导致内存警告甚至应用程序由于内存压力而被迫终止。

另一种方法最好用于所有其他情况。请注意卸载特定资源而不是使用“未使用”方法,因为另一个场景当前可能正在使用该资源,并且如果另一个场景将其删除,则将被迫重新加载它。

PS:只要您的应用程序即使在内存最少的设备上也没有受到内存压力,您不应该仅仅为了防止它们被一次又一次地重新加载而删除资源。使用已经缓存的纹理切换场景会快得多。

于 2013-09-09T20:14:22.253 回答