Cocos2d iphone 去除纹理

Cocos2d iphone 去除纹理,cocos2d-iphone,Cocos2d Iphone,我在场景退出后移除精灵表时遇到一些问题 我基本上按照Ray的指示在init中删除未使用的纹理 [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; [[CCTextureCache sharedTextureCache] removeUnusedTextures]; [[CCDirector sharedDirector] purgeCachedData]; 事实上,我有 CCTexture2D * te

我在场景退出后移除精灵表时遇到一些问题

我基本上按照Ray的指示在init中删除未使用的纹理

[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCDirector sharedDirector] purgeCachedData];
事实上,我有

CCTexture2D * texture = spriteNode.textureAtlas.texture;
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromTexture:texture];
[[CCTextureCache sharedTextureCache] removeTexture:texture];

如果到场景的过渡不是当前场景,则此操作可以正常工作。 但当我试图“重启”当前场景时,它崩溃了

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[currentScene scene] withColor:ccBLACK]];
似乎问题在于,用“新”当前场景替换当前场景时。。在解除分配当前场景之前,将调用“新建”当前场景初始化。因此,我的“新”当前场景精灵表在创建后立即被释放

在这种情况下,我如何正确移除精灵表? 还是我重新启动当前场景的方法不正确

更新: 我试图按照建议添加加载场景,但无法使其正常工作。。这是我的加载场景

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
LoadingLayer * layer = [[[LoadingLayer alloc]init]autorelease];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}


-(id) init{

  if(self = [super init]){

    winSize = [CCDirector sharedDirector].winSize;
    CCLabelTTF * loadingLabel = [CCLabelTTF labelWithString:@"Loading..." fontName:@"Marker Felt" fontSize:30];
    }

    loadingLabel.position = ccp(winSize.width/2, winSize.height/2);
    [loadingLayer addChild:loadingLabel];

    [self scheduleUpdate];

  }
return self;
}

-(void) update:(ccTime)dt{

 [self unscheduleUpdate];
 NSString * bgPlist = @"backgroundsIPAD.plist";;
 NSString * hudPlist = @"hudSpritesIPAD.plist"
 NSString * gameOnePlist = @"gameOneSpritesIPAD.plist";

// load background
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:bgPlist];

// load hud
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:hudPlist];

// load sprites
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:gameOnePlist];

 [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[GameTwoLayer scene] withColor:ccBLACK]];
} 
在这种情况下,这将给我一个GameTowlayer的亮点,然后是一个黑屏。。
我做错了什么?

事实上,新场景的init在当前场景解除分配之前运行。这很有意义,因为您正在当前场景对象中创建一个新的场景对象

因此,在初始化期间不应删除纹理等。这不是一个合适的地方。正在使用这些资源的场景应负责删除这些资源。应使用场景的dealloc方法删除纹理等


如果您需要在两个场景同时处于内存中时避免内存峰值,则必须添加中间“加载”场景,以便在下一个场景开始之前释放上一个场景。

谢谢Steffen。我尝试在中间添加一个加载场景,但结果很奇怪。基本上,我的当前场景调用了LoadingScene。。然后在LoadingScene中,我让它在延迟一段时间后用我的“新”当前场景调用replaceSecene。但我看到的是一个“新”的当前场景,然后是加载屏幕。有什么见解吗?