【问题标题】:Trouble with preloading spritekit textures预加载 spritekit 纹理的问题
【发布时间】:2014-02-08 00:15:57
【问题描述】:

我的初始视图控制器中有这段代码,在函数 viewWillLayoutSubviews 中:

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

我想在我的游戏开始之前加载这些纹理,这样我在游戏过程中就不会出现延迟。然而,这似乎不起作用,因为每当从我的障碍图集中找到的图像制作新的精灵节点时,游戏就会滞后。我是否正确预加载?如果没有,我应该如何以及在哪里这样做。

【问题讨论】:

    标签: xcode sprite-kit preloading


    【解决方案1】:

    那些结构是局部变量,当预加载方法返回时,局部变量被 ARC 释放。您需要保持纹理对象“活跃”(即使它们成为 ivars)以便预加载以达到预期效果。

    此外,如果这些纹理占用大量内存,则随着更多纹理的加载,未使用的纹理可能会从内存中删除。检查任何内存通知。

    【讨论】:

    • 如何让它们全球化?这是在应用程序委托中作为属性完成的吗?
    • 我没有说全局,让它们成为 viewcontroller 的 ivars
    • 所以您只需要加载和保留图像,但您不需要让它们从应用程序的其他地方直接访问?基本上你只是加载它们,所以它们被缓存在 viewController 中,下次你从 SKScene 加载它们时,它们将直接从缓存中加载(与磁盘相对)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多