我在函数视图中的初始视图控制器中具有以下代码WillLayoutSubviews:
SKTextureAtlas *obstacleAtlas = [SKTextureAtlas atlasNamed:@"gonImages"];
obstacleAtlas = [SKTextureAtlas atlasNamed:@"gonImages"];
SKTexture *castle = [obstacleAtlas textureNamed:@"castle@2x.png"];
SKTexture *column = [obstacleAtlas textureNamed:@"columnNE@2x.png"];
SKTexture *factory = [obstacleAtlas textureNamed:@"factory@2x.png"];
SKTexture *hotAirBallon = [obstacleAtlas textureNamed:@"hotAirBallon@2x.png"];
SKTexture *lightningBolt = [obstacleAtlas textureNamed:@"lightningBolt@2x.png"];
SKTexture *pirateShip = [obstacleAtlas textureNamed:@"pirateShip@2x.png"];
SKTexture *pyramidNE = [obstacleAtlas textureNamed:@"PyramidNE@2x.png"];
SKTexture *rocket = [obstacleAtlas textureNamed:@"rocket@2x.png"];
SKTexture *sun = [obstacleAtlas textureNamed:@"sun@2x.png"];
SKTexture *wheel = [obstacleAtlas textureNamed:@"wheel@2x.png"];
NSArray *obstacleTextures = @[castle, column, factory, hotAirBallon, lightningBolt, pirateShip, pyramidNE, rocket, sun, wheel];
[SKTexture preloadTextures:obstacleTextures withCompletionHandler:^{
// Present the scene.
[skView presentScene:startScene];
//[skView presentScene:scene];
}];
我想在游戏开始之前加载这些纹理,这样我在游戏过程中就不会有延迟。然而,这似乎不起作用,因为每当从我的障碍地图集中找到的图像制成的新精灵节点时,游戏就会滞后。我是否正确预加载?如果没有,我应该如何以及在哪里执行此操作。
这些构造是局部变量,当预加载方法返回局部变量时,由 ARC 释放。您需要使纹理对象保持"活动"(即使它们成为ivars)以进行预加载以达到所需的效果。
此外,如果这些纹理消耗大量内存,则随着加载更多纹理,未使用的纹理可能会从内存中删除。检查是否有任何内存通知。