【问题标题】:SKTextureAtlas how to load textures in orderSKTextureAtlas 如何按顺序加载纹理
【发布时间】:2013-12-20 06:53:29
【问题描述】:

我使用 SKTextureAtlas(.atlas 文件夹)来存储我正在使用的动画的帧。我已经对 frame.1.png、frame.2.png 等框架进行了编号 - 总共有 10 个框架。

我注意到我的动画看起来很糟糕,尽管在我的图形程序中预览的帧看起来很棒。我 NSLoged 退出,发现它正在以随机顺序加载图集!我认为它至少会遵循捆绑文件的顺序。我如何让它使用捆绑顺序而不自己对数组进行排序。我还要将@2x 图像与其余图像一起放入还是创建单独的图集?

SKTextureAtlas *sleighAtlas = [SKTextureAtlas atlasNamed:@"sleigh"];
NSArray *textureNames = [sleighAtlas textureNames];
NSMutableArray *sleighTextures = [NSMutableArray new];
for (NSString *name in textureNames) {
    NSLog(@"texture: %@",name);
    SKTexture *texture = [sleighAtlas textureNamed:name];
    [sleighTextures addObject:texture];
}

打印出来:

2013-12-04 09:56:54.407 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.3.png
2013-12-04 09:56:54.407 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.6@2x.png
2013-12-04 09:56:54.408 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.8.png
2013-12-04 09:56:54.408 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.5@2x.png
2013-12-04 09:56:54.408 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.4.png
2013-12-04 09:56:54.408 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.9.png
2013-12-04 09:56:54.409 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.4@2x.png
2013-12-04 09:56:54.409 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.9@2x.png
2013-12-04 09:56:54.409 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.5.png
2013-12-04 09:56:54.410 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.1.png
2013-12-04 09:56:54.410 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.3@2x.png
2013-12-04 09:56:54.410 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.6.png
2013-12-04 09:56:54.410 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.8@2x.png
2013-12-04 09:56:54.411 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.2@2x.png
2013-12-04 09:56:54.411 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.2.png
2013-12-04 09:56:54.455 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.7@2x.png
2013-12-04 09:56:54.456 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.1@2x.png
2013-12-04 09:56:54.456 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.10.png
2013-12-04 09:56:54.456 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.10@2x.png
2013-12-04 09:56:54.457 圣诞老人游戏[41611:70b] 纹理:santaAndSleigh.7.png

谢谢!

【问题讨论】:

    标签: ios sprite-kit


    【解决方案1】:

    可能textureNames 属性包含无序的纹理名称。而不是使用快速枚举使用for(;;):

    for (int i=0; i < sleighAtlas.textureNames.count; i++) {
        NSString *textureName = [NSString stringWithFormat:@"santaAndSleigh.%d", i];
        [sleighTextures addObject:[explosion2Atlas textureNamed:textureName]];
    }
    

    虽然如果你将@2x 图像放入sleighAtlas,这种方法可能不起作用。

    【讨论】:

    • 您始终可以将纹理数量分成两半,因此您只会加载设备所需的资源(视网膜或非视网膜)。 sleighAtlas.textureNames.count / 2
    【解决方案2】:

    func getTexturesByAtlas(_ atlasName: String) -> [SKTexture] { let atlas = SKTextureAtlas(named: atlasName) return atlas.textureNames.sorted().map { name in atlas.textureNamed(name) } }

    【讨论】:

    • 通常对您的答案进行更多解释会有所帮助
    • 这是最好的答案,遗憾的是编辑队列已满,无法正确显示并添加代码注释。请注意,它不仅返回名称,还返回 SKTexture
    【解决方案3】:

    您必须先对纹理名称数组进行排序。

    SKTextureAtlas *sleighAtlas = [SKTextureAtlas atlasNamed:@"sleigh"];
    NSArray * textureNames = [[atlas textureNames] sortedArrayUsingSelector: @selector(compare:)];
    NSMutableArray *sleighTextures = [NSMutableArray new];
    
    for (NSString *name in textureNames) {
    
        [sleighTextures addObject: [sleighAtlas textureNamed: name]];
    }
    

    【讨论】:

      【解决方案4】:

      或者只是使用sortedArrayUsingSelector:localizedCaseInsensitiveCompare 之类的东西对textureNames 数组进行排序。但是,包含 @2x 文件可能会变得不可靠。

      【讨论】:

        【解决方案5】:
        func loadFramesFromAtlasWithName(atlasName: String) -> [SKTexture] {
            let atlas = SKTextureAtlas(named: atlasName)
            var atlasTextures=[SKTexture]()
            for var x=0; x<atlas.textureNames.count; ++x{
                 var texture = atlas.textureNamed(atlasName + "\(x)" + ".png")
                 atlasTextures.append(texture)
                print(atlasName)
        
            }
        
            return atlasTextures
        }
        

        这是我用 swift 使用的。我将文件夹中的图片命名为与文件夹相同的名称,因此文件夹被标记为whale_swimming.atlas,而里面的图片被命名为whale_swimming.png。这是我发布的代码正常工作所必需的

        【讨论】:

          【解决方案6】:
          var animTextures = Array<SKTexture>()
          let animAtlas = SKTextureAtlas(named: "anim")
          for index in 1...animAtlas.textureNames.count {
              let frameName = String(format:"anim-%d", index)
              animTextures.append(animAtlas.textureNamed(frameName))
              print(frameName)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-11
            相关资源
            最近更新 更多