SKTexture preload

When you preload textures using the spritekit preloadTextures function, it simultaneously loads all the textures in the provided array into memory.

If you don’t have the ability to split the levels in your game with “loading screens”, but you have separate levels with different image files than each other, how can you not immediately store all the images in memory without sacrificing frame rates when spritekit loads images when necessary?

+2
source share
1 answer

You can create a singleton class with methods for loading and unloading resources specific to the level you are currently playing. For example, if you have textures a, b, and c that need to be loaded for level one, and textures x, y, and z for level 2, you can have a method -(void)loadLevelOneTextures;and -(void)loadLevelTwoTextures;, as well as a -(void)unloadLevelOneTextures;and-(void)unloadLevelTwoTextures;

So you can say that singleton loads the textures before you need them, and when you're done, you will tell them to release them.

//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;

And implementation:

//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];
}

You would do this for every level that you have, and then to access the textures you would do something like this. (Be sure to import GameTextureLoader.h first)

GameTextureLoader *loader = [GameTextureLoader sharedTextures];
[loader loadLevelOneTextures];
SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:loader.levelOneTextures[0]];
[self addChild:node];
+1
source

Source: https://habr.com/ru/post/1529674/


All Articles