我使用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
谢谢!
答案 0 :(得分:3)
可能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
,这种方法可能不起作用。
答案 1 :(得分:2)
您必须先对textureNames数组进行排序。
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]];
}
答案 2 :(得分:2)
func getTexturesByAtlas(_ atlasName: String) -> [SKTexture] {
let atlas = SKTextureAtlas(named: atlasName)
return atlas.textureNames.sorted().map { name in atlas.textureNamed(name) }
}
答案 3 :(得分:0)
或者只是使用textureNames
和sortedArrayUsingSelector:
之类的内容对localizedCaseInsensitiveCompare
数组进行排序。可能会因为包含@ 2x文件而感到不安。
答案 4 :(得分:0)
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,内部的pic名为whale_swimming.png。这对于我发布的代码来说是必要的
答案 5 :(得分:0)
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)
}