本文介绍了SKTexture预加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

使用spritekit preloadTextures函数预加载纹理时,它将立即将提供的数组中的所有纹理加载到内存中.

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

如果您没有能力通过加载屏幕"在游戏中拆分关卡,但确实具有彼此不同的关卡,且图像文件彼此不同,那么如何避免一次将所有图像存储在内存中Spritekit在需要时加载图像时又不牺牲帧速率吗?

If you don't have the ability to split up your levels in your game with 'loading screens' but do have separate levels with different image files than each other, how can you keep from storing all the images in memory at once without sacrificing frame rate when spritekit loads the images when it needs to?

推荐答案

您可以创建一个单例类,该类具有用于加载和卸载当前正在播放的级别的资源的方法.例如,如果您需要为级别1加载纹理a,b和c,为级别2加载纹理x,y和z,则可以使用方法-(void)loadLevelOneTextures;-(void)loadLevelTwoTextures;以及-(void)unloadLevelOneTextures;-(void)unloadLevelTwoTextures;

You could create a singleton class with methods for loading and unloading resources specific to the level you are currently playing. For instance, 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 could have a method -(void)loadLevelOneTextures; and a -(void)loadLevelTwoTextures; as well as a -(void)unloadLevelOneTextures; and -(void)unloadLevelTwoTextures;

这样,您可以告诉单例在需要纹理之前先加载它们,完成后告诉它释放它们.

This way, you can tell the singleton to load the textures before you need them, and when you're done you tell it 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;

执行:

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

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

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

这篇关于SKTexture预加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:26