【问题标题】:Cannot align images in array (atlas) - SpriteKit无法对齐数组(图集)中的图像 - SpriteKit
【发布时间】:2017-02-27 19:51:12
【问题描述】:

我想要按 win1、win2、win3 顺序从 atlas 纹理制作简单动画。我的代码:

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)   

...

Image.run(SKAction.repeatForever(SKAction.animate(with: ImageArray, timePerFrame : 0.01)))

...

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

【问题讨论】:

  • 那么win3是先出现的吗?

标签: ios arrays swift xcode for-loop


【解决方案1】:

SKTextureAtlas.textureNames 不承诺数组的顺序。如果您希望它们按特定顺序排列,则应自行排序。

您的代码中还有一些样式问题——请注意,变量名称应该采用 lowerCamelCase,否则其他人(以及诸如 SO 的语法高亮器之类的工具)将很难将它们与类型区分开来。这是修复几个问题的改装:

// 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)   

}

...

// use implicit member lookup for terser syntax
imageNode.run(.repeatForever(.animate(with: textures, timePerFrame: 0.01)))

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

【讨论】:

  • 我很绝望:(我的代码和你写的一模一样,但还是不行。我/*评论*/你的代码周围的所有其他代码。NSLog仍然显示:[“win6.png” 、“win1.png”、“win9.png”、“win4.png”、“win7.png”、“win2.png”、“win5.png”、“win8.png”、“win3.png”]。 .. 所有代码都在 GameScene.swift ...
【解决方案2】:

在创建动画时,以下内容通常对我很有效。

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])


   }



 }

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2016-03-30
      相关资源
      最近更新 更多