Ios 无法对齐阵列(atlas)中的图像-SpriteKit

Ios 无法对齐阵列(atlas)中的图像-SpriteKit,ios,arrays,swift,xcode,for-loop,Ios,Arrays,Swift,Xcode,For Loop,我想要简单的动画从阿特拉斯纹理顺序win1,win2,win3。我的代码: var Image = SKSpriteNode() var ImageAtlas = SKTextureAtlas() var ImageArray = [SKTexture]() override func didMove(to view: SKView) { ImageAtlas = SKTextureAtlas(named: "Images") for i in 1...ImageAtlas.

我想要简单的动画从阿特拉斯纹理顺序win1,win2,win3。我的代码:

var Image = SKSpriteNode()
var ImageAtlas = SKTextureAtlas()
var ImageArray = [SKTexture]()

override func didMove(to view: SKView) {

    ImageAtlas = SKTextureAtlas(named: "Images")
    for i in 1...ImageAtlas.textureNames.count{
        let textureName = "win\(i).png"
        let texture = ImageAtlas.textureNamed(textureName)
        ImageArray.append(texture)
    }

    let firstFrame = ImageArray[0]
    Image = SKSpriteNode(texture: firstFrame)
    Image.setScale(0.7)
    Image.position = CGPoint(x: 0, y: self.frame.width / -1.5)
    Image.zPosition = 2

    self.addChild(Image)   

播放动画时,纹理不在对齐win1、win2、win3中,而是在例如win3、win1、win2中。怎么了?谢谢

SKTextureAtlas.textureNames不对数组的顺序做出任何承诺。如果你想让它们按一定的顺序排列,你应该自己排序

代码中还存在一些样式问题—请注意,变量名应该是小写的,否则其他人和工具(如SO的syntax highlighter)将很难将它们与类型区分开来。以下是修复几件事情的改装:

// don't allocate dummy values, just leave these nil until loaded
var imageNode: SKSpriteNode!
var atlas: SKTextureAtlas!
var textures: [SKTexture]!

override func didMove(to view: SKView) {

    atlas = SKTextureAtlas(named: "Images")

    // sort array, convert from names to textures with map
    textures = atlas.textureNames.sorted().map { atlas.textureNamed($0) }

    guard let firstFrame = textures.first
        else { fatalError("missing textures") }

    imageNode = SKSpriteNode(texture: firstFrame)
    imageNode.setScale(0.7)
    imageNode.position = CGPoint(x: 0, y: self.frame.width / -1.5)
    imageNode.zPosition = 2
    self.addChild(imageNode)   

}


这里唯一需要注意的是,Swift标准库的排序是对字符串的一种简单排序:如果您有10多个名称为win1、win2、…、win10、win11的纹理,它会将它们排序为win1、win10、win11、win2、。。。。您可以通过使用NSArray中使用区域设置感知比较的一些桥接排序方法来纠正这一问题。

在创建动画时,以下方法通常对我很有效

class GameScene: SKScene {

   var TextureAtlas = SKTextureAtlas()
   var playerArray = [SKTexture]()

override func didMove(to view: SKView) {

                   //just replace "playerWon" with the name of your atlas folder
   TextureAtlas = SKTextureAtlas(named: "playerWon") 
   for i in 1...TextureAtlas.textureNames.count {
       let Name = "win\(i).png" //replace "win" with whatever name your .png files have
       playerArray.append(SKTexture(imageNamed: Name))
     }

   let playerNode = SKSpriteNode(imageNamed: TextureAtlas.textureNames[0])


   }



 }

终于!两种代码都有效!谢谢对我来说,它不起作用,因为在atlas中缓存了旧信息。我必须使用清洁功能。Product->Clean.

那么win3是第一个出现的吗?我很绝望:我有和你写的一模一样的代码,但仍然不能工作。I/*注释*/代码周围的所有其他代码。NSLog仍然显示:[win6.png、win1.png、win9.png、win4.png、win7.png、win2.png、win5.png、win8.png、win3.png]。。。所有代码都在GameSecene.swift中。。。
// use implicit member lookup for terser syntax
imageNode.run(.repeatForever(.animate(with: textures, timePerFrame: 0.01)))
class GameScene: SKScene {

   var TextureAtlas = SKTextureAtlas()
   var playerArray = [SKTexture]()

override func didMove(to view: SKView) {

                   //just replace "playerWon" with the name of your atlas folder
   TextureAtlas = SKTextureAtlas(named: "playerWon") 
   for i in 1...TextureAtlas.textureNames.count {
       let Name = "win\(i).png" //replace "win" with whatever name your .png files have
       playerArray.append(SKTexture(imageNamed: Name))
     }

   let playerNode = SKSpriteNode(imageNamed: TextureAtlas.textureNames[0])


   }



 }