Вы можете создать одноэлементный класс с методами загрузки и выгрузки ресурсов, соответствующих уровню, который вы сейчас играете. Например, если у вас есть текстуры a, b и c, которые должны быть загружены для первого уровня, а текстуры x, y и z для уровня 2, вы могли бы иметь метод -(void)loadLevelOneTextures;
и -(void)loadLevelTwoTextures;
, а также -(void)unloadLevelOneTextures;
и -(void)unloadLevelTwoTextures;
Таким образом, вы можете сказать, что синглтон загружает текстуры, прежде чем они вам понадобятся, и когда вы закончите, вы скажете, чтобы они выпустили их.
//GameTextureLoader.h
//
@import SpriteKit;
@import Foundation;
@interface GameTextureLoader : NSObject
@property (strong, nonatomic)NSMutableArray *levelOneTextures;
@property (strong, nonatomic)NSMutableArray *levelTwoTextures;
+ (GameTextureLoader *)sharedTextures;
- (void)loadLevelOneTextures;
- (void)unloadLevelOneTextures;
- (void)loadLevelTwoTextures;
- (void)unloadLevelTwoTextures;
И реализация:
//GameTextureLoader.m
//
#import "GameTextureLoader.h"
@implementation GameTextureLoader
+ (GameTextureLoader *)sharedTextures{
static dispatch_once_t onceToken;
static id sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (instancetype)init{
self = [super init];
if (self)
{
self.levelOneTextures = [[NSMutableArray alloc] init];
self.levelTwoTextures = [[NSMutableArray alloc] init];
return self;
}
else{
exit(1);
}
}
- (void)loadLevelOneTextures{
//Order of images will determin order of textures
NSArray *levelOneImageNames = @[@"imageA", @"imageB", @"imageC"];
for (NSString *image in levelOneImageNames){
SKTexture *texture = [SKTexture textureWithImageNamed:image];
[self.levelOneTextures addObject:texture];
}
}
- (void)loadLevelTwoTextures{
//Order of images will determin order of textures
NSArray *levelTwoImageNames = @[@"imageX", @"imageY", @"imageZ"];
for (NSString *image in levelTwoImageNames){
SKTexture *texture = [SKTexture textureWithImageNamed:image];
[self.levelTwoTextures addObject:texture];
}
}
- (void)unloadLevelOneTextures{
[self.levelOneTextures removeAllObjects];
}
- (void)unloadLevelTwoTextures{
[self.levelTwoTextures removeAllObjects];
}
Вы бы сделать это для каждого уровня у вас есть, а затем получить доступ к текстурам вы могли бы сделать что-то вроде этого. (Обязательно первый импорт GameTextureLoader.h)
GameTextureLoader *loader = [GameTextureLoader sharedTextures];
[loader loadLevelOneTextures];
SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:loader.levelOneTextures[0]];
[self addChild:node];
только загрузить текстуры, необходимые для текущего уровня? Не знаете, что вы хотите знать или пытаетесь сделать. – LearnCocos2D