我正在制作冒险类游戏。在制作这个游戏时我遇到了内存泄漏,所以我决定创建一个预加载器,我称之为SKPreloader。
在我的GameViewController中:
_preloader = [[SKPreloader alloc]init];
[_preloader setDelegate:self];
[_preloader preloadTextureAtlasesForScene:1];
在SKPreloader中:
(void)preloadTextureAtlas:(SKTextureAtlas *)atlas{
[atlas preloadWithCompletionHandler:^void {
NSLog(@"laden...");
if([atlas isEqual:_atlasFotoDistort]){
NSLog(@"loaded foto");
[self preloadTextureAtlas:_atlasFotoDistortWoman];
}else if([atlas isEqual:_atlasFotoDistortWoman]){
NSLog(@"loaded foto woman");
[self preloadTextureAtlas:_atlasTV];
}else if ([atlas isEqual:_atlasTV]){
NSLog(@"loaded tv1");
[self preloadTextureAtlas:_atlasTV2];
}else if ([atlas isEqual:_atlasTV2]){
NSLog(@"loaded tv2");
[self preloadTextureAtlas:_atlasTV3];
}else if ([atlas isEqual:_atlasTV3]){
NSLog(@"loaded tv3");
[self preloadTextureAtlas:_atlasDementia];
}else if ([atlas isEqual:_atlasDementia]){
NSLog(@"loaded dementia");
[self preloadTextureAtlas:_atlasDementiaKeuken];
}else if ([atlas isEqual:_atlasDementiaKeuken]){
NSLog(@"loaded keuken");
[self preloadTextureAtlas:_atlasGordijnLinks];
}else if ([atlas isEqual:_atlasGordijnLinks]){
NSLog(@"loaded gordijnlinks");
[self preloadTextureAtlas:_atlasGordijnRechts];
}else if ([atlas isEqual:_atlasGordijnRechts]){
NSLog(@"loaded gordijnrechts");
[self preloadTextureAtlas:_atlasHond];
}else if ([atlas isEqual:_atlasHond]){
NSLog(@"loaded hond");
[self preloadTextureAtlas:_atlasHondKop];
}else if ([atlas isEqual:_atlasHondKop]){
NSLog(@"loaded hond kop");
[self preloadRestOfTextureAtlasesForScene:1];
}
}];
}
- (void)preloadRestOfTextureAtlasesForScene:(int)sceneNumber{
NSArray *arrayOfAtlases = [self restofTextureAtlasesForScene:sceneNumber];
[SKTextureAtlas preloadTextureAtlases:arrayOfAtlases withCompletionHandler:^void{
NSLog(@"loaded all");
[_delegate didFinishPreloading];
}];
}
以_为前缀的文本是SKTextureAtlases。
在GameViewController中读取委托并启动第一个场景。当SKPreloader到达方法preloadRestOfTextureAtlases时,内存使用量为240mb并保持在那里,最终导致崩溃。
有关我如何解决这些问题并仍然预装地图集的任何帮助?
另外:最大的地图集是16mb。所有小地图集都预先加载到最后一个数组中。