【问题标题】:SKTextureAtlas: More than one atlas confusingSKTextureAtlas:多图集令人困惑
【发布时间】:2014-01-21 14:14:36
【问题描述】:

我在使用多个图集时遇到问题。

例如,我有 main_menu.atlas 和 game.atlas 以及这些场景的图像。在主菜单场景出现之前,我为它准备了图集( [SKTextureAtlas atlasNamed:@"main_menu"] )并且一切正常。但是当我开始游戏并在游戏中准备游戏图集( [SKTextureAtlas atlasNamed:@"game"] )后,我只看到空节点(带有红色交叉的矩形)。没有异常或警告 - 一切正常。

当我将所有游戏资源移至 main_menu.atlas 并删除 game.atlas 时,一切正常 - 我在游戏中看到了精灵。但我想分离图集以进行性能优化。

我使用自己的助手来管理 SpriteKit 纹理。它加载图集并返回我需要的纹理。所以我有这些方法:


- (void) loadTexturesWithName:(NSString*)name {
    name = [[self currentDeviceString] stringByAppendingString:[NSString stringWithFormat:@"_%@", name]];
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:name];
    [self.dictAtlases setObject:atlas forKey:name];
}

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        texture = [atlas textureNamed:string];
        if(texture) {
            return texture;
        }
    }
    return nil; // never returns "nil" in my cases
}

“清洁”没有帮助。 我做错了什么? 提前致谢。

【问题讨论】:

  • 我很确定你只能有一个纹理图集,而对于精灵套件,这通常是动态创建的:developer.apple.com/library/ios/recipes/…
  • 谢谢。但是例如,如果我有两个精灵应该出现在一个场景中,但第一个精灵存储在 ~iphone.1.png 中,第二个精灵存储在 ~iphone.2.png (另一个精灵表)中,这两个精灵表都必须是内存和绘图调用计数会增加吗?我认为如果两个精灵都在一个精灵表中会更好,但是您的链接提供的方法不能保证这一点。
  • 我不认为你有任何选择 - 但它会毫无问题地找到精灵。解决此问题的方法是按照链接中的说明将所有单独的精灵放入项目中,然后让 SpriteKit 构建图集。据我所知,Sprite Kit 不是为与预先提供的图集一起使用而设计的。如果你需要这个,你可能想看看 Cocos2d 或 OpenGLES 解决方案。

标签: ios objective-c sprite-kit sktextureatlas


【解决方案1】:

首先让我声明:你绝对可以使用 2+ 个纹理图集:)

现在发布:

您首先加载菜单图集(首先在 dict 中)然后是游戏图集。当您抓取菜单纹理时,一切都很好。 当您外出抓取游戏纹理时,您首先查看菜单图集(没有可用的图像,因此图集返回在此 doc 中定义的占位符纹理,而不是您期望的 nil。

这段代码应该可以正常工作

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        if([[atlas textureNames] containsObject:string]){
            texture = [atlas textureNamed:string];
            return texture;
        }
    }
    return nil;
}

此外,它可以在不添加 .png 的情况下正常工作:)

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2022-01-17
    • 2018-02-26
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多