【问题标题】:Looping zero-padded numbers in swift - Xcode快速循环零填充数字 - Xcode
【发布时间】:2016-02-18 13:52:30
【问题描述】:

在 SKSpriteNode 类下,您可以执行以下操作来为对象设置动画

private func animate(){
  var playerTextures:[SKTexture] = []
  for i in 1...3 {
    playerTextures.append(SKTexture(imageNamed: "image00\(i)"))
  }
  let playerAnimation = SKAction.repeatActionForever(
    SKAction.animateWithTextures(playerTextures, timePerFrame: 0.1))
  self.runAction(playerAnimation)
}

上面的代码可以使用图像序列为对象设置动画。这里的图像文件是 image001.png image002.png image003.png

这是我的问题,如果文件名是 image001.png image002.png ... image009.png image010.png image011.png image012.png 这里的关键点是零填充的问题。有什么想法吗?

【问题讨论】:

    标签: xcode swift animated leading-zero zero-pad


    【解决方案1】:

    假设您只有三位数字,您可以通过这种方式添加图片:

    for i in 1...3 {
        let imageNumber = String(format: "%03d", i)
        playerTextures.append(SKTexture(imageNamed: "image\(imageNumber)"))
    }
    

    这将为您提供 image001、image002 和 image003

    这要求你在 swift 文件的开头import Foundation

    【讨论】:

    • 谢谢,顺便说一句,你可能打错了 "image(String(format: "%03d", i))"
    • 我编辑了我的答案,我认为现在应该没问题并且更容易阅读;)
    • 请注意,在这里说“谢谢”的首选方式是对好的问题和有用的答案进行投票(一旦你有足够的声誉这样做),并接受最有帮助的答案您提出的任何问题(这也会稍微提升您的声誉)。请参阅About 页面和How do I ask questions here?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2021-03-02
    • 1970-01-01
    • 2010-12-13
    • 2015-05-19
    • 2018-01-23
    相关资源
    最近更新 更多