我在场景退出后删除精灵表时遇到了一些问题。
我基本上遵循 Ray 的指示,在 init 中删除未使用的纹理
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCDirector sharedDirector] purgeCachedData];
在 dealloc 我有
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]];
似乎问题在于,当用“新”当前场景替换当前场景时。“新”当前场景 init 在当前场景被释放之前被调用。因此,我的“新”当前场景精灵表在创建后立即被释放。
在这种情况下,如何正确删除精灵表?还是我重新启动当前场景的方法不正确?
更新:我试图按照建议添加加载场景,但无法使其工作..这是我的加载场景
+(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]];
}
在这种情况下,这会给我带来 GameTwoLayer 的飞溅,然后是黑屏。我做错了什么?