Spritekit- SKTextureatlas - 在多个场景中使用预加载的纹理
我一直在研究过去两天对我的问题有一个明确的答案,我根本找不到一个在线。Spritekit- SKTextureatlas - 在多个场景中使用预加载的纹理
我正在开发一款使用精灵套件的IOS游戏,我处于优化阶段,但遇到了问题。我想在应用程序启动时预先加载所有资源,然后在我的应用程序的所有场景和类中使用它们。
我明白这是使用纹理图集的预加载功能完成的。我创建了一个.atlas文件夹名为powerUpTex.atlas,这是我所编写的函数:
首先我添加了一个属性到TitleScene作为数组
@interface TitleScene:SKScene
@property(强)NSArray * textureData;
@end
//我把纹理阵列中加载游戏场景 SKTextureAtlas *图谱=函数[SKTextureAtlas atlasNamed:@ “powerUpTex”];
SKTexture *powerUp1 = [atlas textureNamed: powerUp1ImageName];
SKTexture *powerUp2 = [atlas textureNamed: powerUp2ImageName];
SKTexture *powerUp3 = [atlas textureNamed: powerUp3ImageName];
textureData = [[NSArray alloc] init];
textureData= @[powerUp1,powerUp2,powerUp3];
[SKTexture preloadTextures:textureData withCompletionHandler:^{
NSLog(@"textures loaded");
//Load the GameScene
}
我的应用程序包含以下类: 1.App代表
2.ViewController
3.TitleScene
4.GameScene
5.HelperClass1(被称为通过游戏场景并使用预加载纹理返回SKSprite节点)
6.HelperClass2(由游戏场景调用,并返回使用预装的纹理的SKSprite节点)
7.GameOverScene
好了,所以我的问题是:
首先,我应该放在哪里这个功能(如在什么类中)。在应用程序代表?视图控制器?标题场景?
其次,更重要的是,纹理预先加载到哪里以及如何从任何场景或类中在应用程序中访问它们!如果例如我想使用HelperClass2中的powerUp1纹理初始化一个skspritenode,我该如何在代码中做到这一点?
我一直在试验一天没有成功。我将不胜感激包括我可以使用的代码片段的答复。
您可以将您的纹理作为单例或作为ViewController的公共属性的NSMutableDictionary保存在您的ViewController中,例如,
@interface myViewController : UIViewController
@property NSMutableDictionary *level1Textures;
@property NSMutableDictionary *level2Textures;
@property NSMutableDictionary *level3Textures;
@end
现在你可以通过访问这些的NSMutableDictionary每SKScene =
gameViewController=(UIViewController*)self.view.window.rootViewController
其中gameViewController是UIViewController中的一个实例现在可以访问level1Textures和level2Textures因为他们的UIViewController
的公共属性你有没有想过这个?我正在经历完全的你。在文档或任何可以清楚地找到答案的地方都没有答案。我遇到过的最好的评论是,在App Delegate或VC中加载纹理,并且在那里保存对它们的引用,将允许你在你的场景中创建SKTextures(比如GameScene),它不需要有时候将图像移动到内存中,因为它已经存在于App Delegate(或VC,如果这就是你所做的)。评论不是答案,因为我无法证实它。如果有人愿意,会喜欢它。 – Brad 2016-04-15 21:24:58